[Issue 4239] New: Mixed tuple comparison

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed May 26 14:50:00 PDT 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 at puremagic.com
        ReportedBy: simen.kjaras at gmail.com


--- Comment #0 from Simen Kjaeraas <simen.kjaras at 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: -------


More information about the Digitalmars-d-bugs mailing list