← Back to index
|
Original Bugzilla link
Bug 4239 – Mixed tuple comparison
Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
Other
OS
Windows
Creation time
2010-05-26T14:49:58Z
Last change time
2017-08-25T10:43:21Z
Assigned to
No Owner
Creator
Simen Kjaeraas
Comments
Comment #0
by simen.kjaras — 2010-05-26T14:49:58Z
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")); }
Comment #1
by nfxjfg — 2010-05-26T14:58:55Z
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)));
Comment #2
by simen.kjaras — 2010-05-26T15:20:55Z
(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")); }
Comment #3
by nfxjfg — 2010-05-30T08:36:20Z
Bug 3279 is the reason why the obvious idea to implement this fails.
Comment #4
by razvan.nitu1305 — 2017-08-25T10:43:21Z
This issue is very old and I don't think that there is much need for this feature. Closing as WONTFIX, reopen if the need for this is proven.