www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Comparing Two Type Tuples

reply Daniel Ribeiro Maciel <danielmaciel gmail.com> writes:
Heya ppl!

I was wondering how could I write a function that takes two Type
Tuples as arguments and returns true if they are match.

Could anyone help me with this?

Thanks!
Apr 04 2010
next sibling parent reply Justin Spahr-Summers <Justin.SpahrSummers gmail.com> writes:
On Sun, 4 Apr 2010 21:05:49 +0000 (UTC), Daniel Ribeiro Maciel 
<danielmaciel gmail.com> wrote:
 
 Heya ppl!
 
 I was wondering how could I write a function that takes two Type
 Tuples as arguments and returns true if they are match.
 
 Could anyone help me with this?
 
 Thanks!
You can really only pass a single type tuple to a template, but you can work around it by passing a length and then comparing two parts of a combined tuple. For instance: import std.stdio; import std.typetuple; void compare(uint LEN, TL ...) () { writefln("%s", is(TL[0 .. LEN] == TL[LEN .. $])); } void main() { alias TypeTuple!(int, double, char[]) tupleA; alias TypeTuple!(int, double, char[]) tupleB; alias TypeTuple!(int, double, char*) tupleC; compare!(tupleA.length, tupleA, tupleB); compare!(tupleA.length, tupleA, tupleC); } will output "true" then "false."
Apr 04 2010
next sibling parent Daniel Ribeiro Maciel <danielmaciel gmail.com> writes:
Wow! Nice! Thanks a lot!
Apr 04 2010
prev sibling next sibling parent bearophile <bearophileHUGS lycos.com> writes:
Justin Spahr-Summers:
 void compare(uint LEN, TL ...) () {
     writefln("%s", is(TL[0 .. LEN] == TL[LEN .. $]));
 }
That's the best piece of D code I've seen this week :-) And I didn't know the is(==) works among tuples too. So far Walter has shown no interest in fixing the bad flattening semantics of tuples. Maybe it's a hard thing to implement. Bye, bearophile
Apr 04 2010
prev sibling next sibling parent reply Philippe Sigaud <philippe.sigaud gmail.com> writes:
On Sun, Apr 4, 2010 at 23:23, Justin Spahr-Summers <
Justin.SpahrSummers gmail.com> wrote:

 You can really only pass a single type tuple to a template, but you can
 work around it by passing a length and then comparing two parts of a
 combined tuple. For instance:

 import std.stdio;
 import std.typetuple;

 void compare(uint LEN, TL ...) () {
    writefln("%s", is(TL[0 .. LEN] == TL[LEN .. $]));
 }
Since to be equal, they must have the same length, you can even get rid of the 'len' part and cut in the middle: bool compare(TT...)() { static if (TT.length % 2 == 0 && is(TT[0..$/2] == TT[$/2..$])) return true; else return false; } That's useful to compare two functions: template sameArgs(alias fun1, alias fun2) { static if (compare!(ParameterTypeTuple!fun1, ParameterTypeTuple!fun2)) enum bool sameArgs = true; else enum bool sameArgs = false; } (I think we could use std.traits.Select to manage this kind of static ifs) Philippe
Apr 05 2010
parent reply bearophile <bearophileHUGS lycos.com> writes:
Philippe Sigaud:
 Since to be equal, they must have the same length, you can even get rid of
 the 'len' part and cut in the middle:
But if their length is not equal that can cause bugs. Bye, bearophile
Apr 05 2010
parent Philippe Sigaud <philippe.sigaud gmail.com> writes:
On Mon, Apr 5, 2010 at 13:47, bearophile <bearophileHUGS lycos.com> wrote:

 Philippe Sigaud:
 Since to be equal, they must have the same length, you can even get rid
of
 the 'len' part and cut in the middle:
But if their length is not equal that can cause bugs.
Uh, next time I'll try to eat before posting. Indeed, my template would say that (int,int) and (double, int, int, double) are equal. Doh, I'll go and find some chocolate to go with my cappuccino. Philippe
Apr 05 2010
prev sibling parent Philippe Sigaud <philippe.sigaud gmail.com> writes:
On Mon, Apr 5, 2010 at 12:52, Philippe Sigaud <philippe.sigaud gmail.com>wrote:


 That's useful to compare two functions:

 template sameArgs(alias fun1, alias fun2) {
     static if (compare!(ParameterTypeTuple!fun1, ParameterTypeTuple!fun2))
Ugh, forget this example, in this case we know the original tuples, so == is simpler and cleaner. Another interesting thing to test is if one typetuple can be converted into another. Maybe like this: bool compatibleTypeTuples(TT...) { TT[0..$/2] tt1; TT[$/2..$] tt2; static if (__traits(compiles, tt1 = tt2)) return true; else return false; }
Apr 05 2010
prev sibling next sibling parent Robert Clipsham <robert octarineparrot.com> writes:
On 04/04/10 22:05, Daniel Ribeiro Maciel wrote:
 Heya ppl!

 I was wondering how could I write a function that takes two Type
 Tuples as arguments and returns true if they are match.

 Could anyone help me with this?

 Thanks!
Based on Justin's code, I came up with this to remove the need to pass the length of the TypeTuple: ---- import std.typetuple; alias TypeTuple!(int, double, char[]) tupleA; alias TypeTuple!(int, double, char[]) tupleB; alias TypeTuple!(int, double, char*) tupleC; // Method 1: write an is() each time pragma(msg, (is(tupleA == tupleB)).stringof); pragma(msg, (is(tupleA == tupleC)).stringof); template compare(string t1, string t2) { enum compare = mixin("is("~t1~"=="~t2~")"); } // Method 2: Use the above template and pass strings pragma( msg, compare!("tupleA", "tupleB") ); pragma( msg, compare!("tupleA", "tupleC") ); ----
Apr 04 2010
prev sibling next sibling parent reply "Simen kjaeraas" <simen.kjaras gmail.com> writes:
Daniel Ribeiro Maciel <danielmaciel gmail.com> wrote:

 Heya ppl!

 I was wondering how could I write a function that takes two Type
 Tuples as arguments and returns true if they are match.

 Could anyone help me with this?

 Thanks!
This should work, but doesn't (filing a bugzilla about it now): template compareTuple( T... ) { template compareTuple( U... ) { enum bool compareTuple = is( T == U ); } } static assert( compareTuple!( int, float )!( int, float ) ); However, this works: alias compareTuple!( int, float ) foo; static assert( foo!( int, float ) ); -- Simen
Apr 04 2010
next sibling parent "Simen kjaeraas" <simen.kjaras gmail.com> writes:
Simen kjaeraas <simen.kjaras gmail.com> wrote:

 Daniel Ribeiro Maciel <danielmaciel gmail.com> wrote:

 Heya ppl!

 I was wondering how could I write a function that takes two Type
 Tuples as arguments and returns true if they are match.

 Could anyone help me with this?

 Thanks!
This should work, but doesn't (filing a bugzilla about it now): template compareTuple( T... ) { template compareTuple( U... ) { enum bool compareTuple = is( T == U ); } } static assert( compareTuple!( int, float )!( int, float ) ); However, this works: alias compareTuple!( int, float ) foo; static assert( foo!( int, float ) );
There. http://d.puremagic.com/issues/show_bug.cgi?id=4061 -- Simen
Apr 04 2010
prev sibling parent BCS <none anon.com> writes:
Hello Simen,


 This should work, but doesn't (filing a bugzilla about it now):
 
 template compareTuple( T... ) {
 template compareTuple( U... ) {
 enum bool compareTuple = is( T == U );
 }
 }
 static assert( compareTuple!( int, float )!( int, float ) );
It does work if you rename the inner template to something like "with". -- ... <IXOYE><
Apr 04 2010
prev sibling parent reply BCS <none anon.com> writes:
Hello Daniel,

 Heya ppl!
 
 I was wondering how could I write a function that takes two Type
 Tuples as arguments and returns true if they are match.
 
 Could anyone help me with this?
 
 Thanks!
here is my untested vertion: template Compare(T...) { template With(U...) { static if(T.length != U.lenght) const bool With = false; else static if(T.length == 0) const bool With = true; else static if(is(T[0] == U[0])) const bool With = Compare!(T[1..$]).With!(U[1..$]); else const bool With = false; } } use like: Compare!(int, float, myStruct).With(alias1,alias2,typeArg) -- ... <IXOYE><
Apr 04 2010
parent reply Justin Spahr-Summers <Justin.SpahrSummers gmail.com> writes:
On Mon, 5 Apr 2010 02:59:15 +0000 (UTC), BCS <none anon.com> wrote:
 
 Hello Daniel,
 
 Heya ppl!
 
 I was wondering how could I write a function that takes two Type
 Tuples as arguments and returns true if they are match.
 
 Could anyone help me with this?
 
 Thanks!
here is my untested vertion: template Compare(T...) { template With(U...) { static if(T.length != U.lenght) const bool With = false; else static if(T.length == 0) const bool With = true; else static if(is(T[0] == U[0])) const bool With = Compare!(T[1..$]).With!(U[1..$]); else const bool With = false; } } use like: Compare!(int, float, myStruct).With(alias1,alias2,typeArg)
Definitely a lot cleaner. I'm curious, though... is there a reason to avoid is(T == U)?
Apr 05 2010
next sibling parent bearophile <bearophileHUGS lycos.com> writes:
Justin Spahr-Summers:
 Definitely a lot cleaner. I'm curious, though... is there a reason to 
 avoid is(T == U)?
I agree, I think the best answer is to not use a function/template, and just use the is(==) operator: import std.stdio: writeln; import std.typetuple; void main() { alias TypeTuple!(int, double, char[]) A; alias TypeTuple!(int, double, char[]) B; alias TypeTuple!(int, double, char*) C; alias TypeTuple!(int, double) D; writeln(is(A == B)); writeln(is(A == C)); writeln(is(A == D)); } Bye, bearophile
Apr 05 2010
prev sibling parent reply BCS <none anon.com> writes:
Hello Justin Spahr-Summers,

 On Mon, 5 Apr 2010 02:59:15 +0000 (UTC), BCS <none anon.com> wrote:
 
 Hello Daniel,
 
 Heya ppl!
 
 I was wondering how could I write a function that takes two Type
 Tuples as arguments and returns true if they are match.
 
 Could anyone help me with this?
 
 Thanks!
 
here is my untested vertion: template Compare(T...) { template With(U...) { static if(T.length != U.lenght) const bool With = false; else static if(T.length == 0) const bool With = true; else static if(is(T[0] == U[0])) const bool With = Compare!(T[1..$]).With!(U[1..$]); else const bool With = false; } } use like: Compare!(int, float, myStruct).With(alias1,alias2,typeArg)
Definitely a lot cleaner. I'm curious, though... is there a reason to avoid is(T == U)?
I dind't know it worked? -- ... <IXOYE><
Apr 05 2010
parent reply Justin Spahr-Summers <Justin.SpahrSummers gmail.com> writes:
On Mon, 5 Apr 2010 15:47:10 +0000 (UTC), BCS <none anon.com> wrote:
 
 Hello Justin Spahr-Summers,
 
 On Mon, 5 Apr 2010 02:59:15 +0000 (UTC), BCS <none anon.com> wrote:
 
 Hello Daniel,
 
 Heya ppl!
 
 I was wondering how could I write a function that takes two Type
 Tuples as arguments and returns true if they are match.
 
 Could anyone help me with this?
 
 Thanks!
 
here is my untested vertion: template Compare(T...) { template With(U...) { static if(T.length != U.lenght) const bool With = false; else static if(T.length == 0) const bool With = true; else static if(is(T[0] == U[0])) const bool With = Compare!(T[1..$]).With!(U[1..$]); else const bool With = false; } } use like: Compare!(int, float, myStruct).With(alias1,alias2,typeArg)
Definitely a lot cleaner. I'm curious, though... is there a reason to avoid is(T == U)?
I dind't know it worked?
It seemed to when I tested the snippet that I sent, but it might've just been luck of the draw, and in reality fail silently on certain edge cases. I'm really not sure.
Apr 05 2010
parent BCS <none anon.com> writes:
Hello Justin Spahr-Summers,

 On Mon, 5 Apr 2010 15:47:10 +0000 (UTC), BCS <none anon.com> wrote:
 
 I dind't know it worked?
 
It seemed to when I tested the snippet that I sent, but it might've just been luck of the draw, and in reality fail silently on certain edge cases. I'm really not sure.
While it's nice to see people questioning if something exists just because I don't know about it :), I should point out that there are many corners of D I have yet to explore. In this case, given that several people suggested it, I think it does work. (And if not, it should.) -- ... <IXOYE><
Apr 06 2010