digitalmars.D.learn - Comparing Two Type Tuples
- Daniel Ribeiro Maciel (5/5) Apr 04 2010 Heya ppl!
- Justin Spahr-Summers (19/28) Apr 04 2010 You can really only pass a single type tuple to a template, but you can
- Daniel Ribeiro Maciel (1/1) Apr 04 2010 Wow! Nice! Thanks a lot!
- bearophile (6/9) Apr 04 2010 That's the best piece of D code I've seen this week :-)
- Philippe Sigaud (19/27) Apr 05 2010 Since to be equal, they must have the same length, you can even get rid ...
- bearophile (4/6) Apr 05 2010 But if their length is not equal that can cause bugs.
- Philippe Sigaud (5/10) Apr 05 2010 Uh, next time I'll try to eat before posting. Indeed, my template would ...
- Philippe Sigaud (14/17) Apr 05 2010 Ugh, forget this example, in this case we know the original tuples, so =...
- Robert Clipsham (19/24) Apr 04 2010 Based on Justin's code, I came up with this to remove the need to pass
- Simen kjaeraas (13/18) Apr 04 2010 This should work, but doesn't (filing a bugzilla about it now):
- Simen kjaeraas (5/24) Apr 04 2010 There.
- BCS (4/12) Apr 04 2010 It does work if you rename the inner template to something like "with".
- BCS (16/24) Apr 04 2010 here is my untested vertion:
- Justin Spahr-Summers (3/31) Apr 05 2010 Definitely a lot cleaner. I'm curious, though... is there a reason to
- bearophile (15/17) Apr 05 2010 I agree, I think the best answer is to not use a function/template, and ...
- BCS (4/35) Apr 05 2010 I dind't know it worked?
- Justin Spahr-Summers (4/40) Apr 05 2010 It seemed to when I tested the snippet that I sent, but it might've just...
- BCS (8/16) Apr 06 2010 While it's nice to see people questioning if something exists just becau...
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
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
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
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
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
On Mon, Apr 5, 2010 at 13:47, bearophile <bearophileHUGS lycos.com> wrote:Philippe Sigaud: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. PhilippeSince to be equal, they must have the same length, you can even get ridofthe 'len' part and cut in the middle:But if their length is not equal that can cause bugs.
Apr 05 2010
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
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
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
Simen kjaeraas <simen.kjaras gmail.com> wrote:Daniel Ribeiro Maciel <danielmaciel gmail.com> wrote:There. http://d.puremagic.com/issues/show_bug.cgi?id=4061 -- SimenHeya 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 ) );
Apr 04 2010
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
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
On Mon, 5 Apr 2010 02:59:15 +0000 (UTC), BCS <none anon.com> wrote:Hello Daniel,Definitely a lot cleaner. I'm curious, though... is there a reason to avoid is(T == U)?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)
Apr 05 2010
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
Hello Justin Spahr-Summers,On Mon, 5 Apr 2010 02:59:15 +0000 (UTC), BCS <none anon.com> wrote:I dind't know it worked? -- ... <IXOYE><Hello Daniel,Definitely a lot cleaner. I'm curious, though... is there a reason to avoid is(T == U)?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)
Apr 05 2010
On Mon, 5 Apr 2010 15:47:10 +0000 (UTC), BCS <none anon.com> wrote:Hello Justin Spahr-Summers,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.On Mon, 5 Apr 2010 02:59:15 +0000 (UTC), BCS <none anon.com> wrote:I dind't know it worked?Hello Daniel,Definitely a lot cleaner. I'm curious, though... is there a reason to avoid is(T == U)?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)
Apr 05 2010
Hello Justin Spahr-Summers,On Mon, 5 Apr 2010 15:47:10 +0000 (UTC), BCS <none anon.com> wrote: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><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 06 2010