Alias/Ref Tuples ?

Philippe Sigaud philippe.sigaud at gmail.com
Sat Dec 17 02:13:30 PST 2011


On Sat, Dec 17, 2011 at 01:23, Simen Kjærås <simen.kjaras at gmail.com> wrote:
> A few small tests later:
>
> import std.typetuple;
> import std.typecons;
> import std.stdio;
>
> void main() {
>    int a, b;
>    TypeTuple!(a, b) = tuple(4,5);
>
>    assert(a == 4 && b == 5);
> }
>
> In other words, the language already has this.

Wow. How does that work?  I'd understand:

TypeTuple!(a,b) = tuple(a,b).expand; // Or .tupleof, even.

but not your example... Does that mean TypeTuple!() = does some destructuring?

Let's do some test:

struct Pair1 { TypeTuple!(int,int) value; }
struct Pair2 { TypeTuple!(int,int) value; alias value this;}

void main() {

    int a, b;
    a = 1;
    b = 2;

//  TypeTuple!(a, b) = Pair1(b, a+b); // boom!
    TypeTuple!(a, b) = Pair2(b, a+b); // works!
    writeln(a," ",b);
}

So it's a side effect of alias this and tuples...


More information about the Digitalmars-d-learn mailing list