Multiple return values...

Derek ddparnell at bigpond.com
Thu Mar 15 02:52:13 PDT 2012


On Thu, 15 Mar 2012 19:23:57 +1100, Timon Gehr <timon.gehr at gmx.ch> wrote:
>> I'd like to break the nexus between science and magic here.
>>
>
> // this is used to access language built-in tuples
> template to(T...) {
>      alias T to;
> }
>
> // this builds a struct akin to std.typecons.tuple
> // it contains the function parameters as fields t[0], t[1],...
> auto from(T...)(T t) {
>      struct Result { T t; alias t this; }
>      return Result( t );
> }
>
> // to!(a,b) creates a tuple containing aliases to a and b
>
> to!(a,b) = from(b,a);   // cannot assign Result to two fields
> to!(a,b) = from(b,a).t; // try alias this
> auto __tmp = from(b,a).t; // expand tuple assign to multiple assignment
> a[0] = __tmp[0], a[1] = __tmp[1];

Thanks, but I was hoping more for an explanation in English.

Are you saying that the generated code is something like ...

    struct __tmpS { int Fa, int Fb };
    __tempS __tmp;
    __tmp.Fa = a;
    __tmp.Fb = b;
    a = __tmp.Fb;
    b = __tmp.Fa;


-- 
Derek Parnell
Melbourne, Australia


More information about the Digitalmars-d mailing list