Multiple return values...

Iain Buclaw ibuclaw at ubuntu.com
Thu Mar 15 00:53:25 PDT 2012


On 14 March 2012 22:06, Derek Parnell <ddparnell at bigpond.com> wrote:
> On Thu, 15 Mar 2012 08:52:26 +1100, Andrei Alexandrescu
> <SeeWebsiteForEmail at erdani.org> wrote:
>
>> On 3/14/12 3:00 PM, Simen Kjærås wrote:
>>>
>>> template to(T...) {
>>> alias T to;
>>> }
>>>
>>> auto from(T...)(T t) {
>>> struct Result { T t; alias t this; }
>>> return Result( t );
>>> }
>>>
>>> void main( ) {
>>> int a = 3;
>>> int b = 4;
>>>
>>> to!(a, b) = from(b, a);
>>>
>>> assert( a == 4 );
>>> assert( b == 3 );
>>> }
>>
>>
>> I got reborn inside a little when seeing this code. Congratulations!
>
>
> And I died a little ... I have no idea what this code is doing. What is the
> generated code produced by this and why?
>
> I'd like to break the nexus between science and magic here.
>
> --
> Derek

It gets expanded more or less to this:

// auto from(T...)(T t)
Result from (int a, int b) {
  return {.field0 = a, .field1 = b};
}

void main() {
  int a = 3;
  int b = 4;

// to!(b, a) = from(a, b)
  ref Result __tup = &from(a, b),
  a = __tup->field0,
  b = __tup->field1;
}


Regards
-- 
Iain Buclaw

*(p < e ? p++ : p) = (c & 0x0f) + '0';


More information about the Digitalmars-d mailing list