April 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
April 05, 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


April 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
April 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;
}


April 05, 2010
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


April 05, 2010
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><



April 06, 2010
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.
April 06, 2010
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><



1 2
Next ›   Last »