www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 4239] New: Mixed tuple comparison

reply d-bugmail puremagic.com writes:
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



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
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4239


nfxjfg gmail.com changed:

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



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
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4239




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)));
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 26 2010
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4239


nfxjfg gmail.com changed:

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



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: -------
May 30 2010