Thread overview
[Issue 4239] New: Mixed tuple comparison
May 26, 2010
Simen Kjaeraas
May 26, 2010
nfxjfg@gmail.com
May 26, 2010
Simen Kjaeraas
May 30, 2010
nfxjfg@gmail.com
May 26, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4239

           Summary: Mixed tuple comparison
           Product: D
           Version: future
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody@puremagic.com
        ReportedBy: simen.kjaras@gmail.com


--- Comment #0 from Simen Kjaeraas <simen.kjaras@gmail.com> 2010-05-26 14:49:58 PDT ---
I have been unable to find a way to compare two tuples of mixed content, like (int, "foo"). For this purpose, I have created the following template, and request its inclusion in Phobos.

/**
Compares tuples with a mixture of types and values.

Example:
----
static assert(SameTuple!(int, int).As!(int, int));
static assert(SameTuple!(int, "foo").As!(int, "foo"));
static assert(!SameTuple!(int, "foo").As!("foo", int));
----
 */

template SameTuple(T...) {
    alias SameTupleImpl!T SameTuple;
}

template SameTupleImpl(T...) if (T.length == 1) {
    template As(U...) if (U.length == 1) {
        static if (is(typeof( T[0])) && is(typeof(U[0]))) {
            enum As = T[0] == U[0];
        } else static if (!is(typeof( T)) && !is(typeof(U[0]))) {
            enum As = is(T[0] == U[0]);
        } else {
            enum As = false;
        }
    }
    template As(U...) if (U.length != 1) {
        enum As = false;
    }
}

template SameTupleImpl(T...) if (T.length != 1) {
    template As(U...) {
        static if (T.length != U.length) {
            enum As = false;
        } else static if (T.length == 0) {
            enum As = true;
        } else {
            enum As = SameTuple!(T[0]).As!(U[0]) &&
SameTuple!(T[1..$]).As!(U[1..$]);
        }
    }
}

unittest {
    static assert(SameTuple!(int, int).As!(int, int));
    static assert(SameTuple!(float).As!(float));
    static assert(SameTuple!("foo").As!("foo"));
    static assert(!SameTuple!("foo").As!("bar"));
    static assert(!SameTuple!(int ).As!("bar"));
    static assert(!SameTuple!(int ).As!(float));
    static assert(SameTuple!(int, "foo").As!(int, "foo"));
    static assert(!SameTuple!(int, "foo").As!("foo", int));
    static assert(SameTuple!().As!());
    static assert(!SameTuple!(int).As!());
    static assert(!SameTuple!().As!(int));
    static assert(!SameTuple!("foo").As!());
    static assert(!SameTuple!().As!("foo"));
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 26, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4239


nfxjfg@gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |nfxjfg@gmail.com


--- Comment #1 from nfxjfg@gmail.com 2010-05-26 14:58:55 PDT ---
This seems to work fine:

struct X(T...) { }

static assert(is(X!(int, int) == X!(int, int)));
static assert(is(X!(int, "foo") == X!(int, "foo")));
static assert(!is(X!(int, "foo") == X!("foo", int)));

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 26, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4239



--- Comment #2 from Simen Kjaeraas <simen.kjaras@gmail.com> 2010-05-26 15:20:55 PDT ---
(In reply to comment #1)
> This seems to work fine:
> 
> struct X(T...) { }
> 
> static assert(is(X!(int, int) == X!(int, int)));
> static assert(is(X!(int, "foo") == X!(int, "foo")));
> static assert(!is(X!(int, "foo") == X!("foo", int)));

Indeed it does. I do however still feel it should be included in Phobos as
something more obvious.
Simplified version, with newly acquired knowledge:

/**
Compares tuples that might contain a mixture of types and values.

Example:
----
static assert(SameTuple!(int, int).As!(int, int));
static assert(SameTuple!(int, "foo").As!(int, "foo"));
static assert(!SameTuple!(int, "foo").As!("foo", int));
----
 */

struct SameTupleImpl(T...) {
}

template SameTuple(T...) {
    template As(U...) {
        enum As = is( SameTupleImpl!T == SameTupleImpl!U );
    }
}

unittest {
    static assert(SameTuple!(int, int).As!(int, int));
    static assert(SameTuple!(float).As!(float));
    static assert(SameTuple!("foo").As!("foo"));
    static assert(!SameTuple!("foo").As!("bar"));
    static assert(!SameTuple!(int ).As!("bar"));
    static assert(!SameTuple!(int ).As!(float));
    static assert(SameTuple!(int, "foo").As!(int, "foo"));
    static assert(!SameTuple!(int, "foo").As!("foo", int));
    static assert(SameTuple!().As!());
    static assert(!SameTuple!(int).As!());
    static assert(!SameTuple!().As!(int));
    static assert(!SameTuple!("foo").As!());
    static assert(!SameTuple!().As!("foo"));
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 30, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4239


nfxjfg@gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Depends on|                            |3279


--- Comment #3 from nfxjfg@gmail.com 2010-05-30 08:36:20 PDT ---
Bug 3279 is the reason why the obvious idea to implement this fails.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------