Thread overview | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
April 04, 2010 Comparing Two Type Tuples | ||||
---|---|---|---|---|
| ||||
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! |
April 04, 2010 Re: Comparing Two Type Tuples | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Ribeiro Maciel | 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."
|
April 04, 2010 Re: Comparing Two Type Tuples | ||||
---|---|---|---|---|
| ||||
Posted in reply to Justin Spahr-Summers | Wow! Nice! Thanks a lot! |
April 04, 2010 Re: Comparing Two Type Tuples | ||||
---|---|---|---|---|
| ||||
Posted in reply to Justin Spahr-Summers | 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
|
April 04, 2010 Re: Comparing Two Type Tuples | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Ribeiro Maciel | 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") );
----
|
April 05, 2010 Re: Comparing Two Type Tuples | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Ribeiro Maciel | 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 |
April 05, 2010 Re: Comparing Two Type Tuples | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simen kjaeraas | 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 |
April 05, 2010 Re: Comparing Two Type Tuples | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Ribeiro Maciel | 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>< |
April 05, 2010 Re: Comparing Two Type Tuples | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simen kjaeraas | 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>< |
April 05, 2010 Re: Comparing Two Type Tuples | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | 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)?
|
Copyright © 1999-2021 by the D Language Foundation