Multiple return values...

Robert Jacques sandford at jhu.edu
Sat Mar 10 17:29:11 PST 2012


On Sat, 10 Mar 2012 09:58:30 -0600, bearophile <bearophileHUGS at lycos.com> wrote:

> Robert Jacques:
>
>> The proposed unpacking syntax, IIRC, was:
>>
>> (double a, int b) = fun();
>>
>> which lowered to:
>>
>> auto __tup = fun();
>> double x = __tup[0];  // __tup[0] has a type that is implicitly convertible to double
>> int y = __tup[1];  // ditto
>>
>> Why not instead lower to:
>>
>> auto __tup = fun();
>> double x = __tup.tupleof[0];
>> int y = __tup.tupleof[1];
>
> The advange of lowering it to:
>> auto __tup = fun();
>> double x = __tup[0];
>
> Is that it works with this too:
> int[2] foo() { return [10,20; }
> auto (x,y) = foo();
>
> This case is quite common.
>
> Bye,
> bearophile
>

And should not tupleof work with int[2] as well? Ever since int[2] switched to being by value, as opposed to by reference it has behaved, under the hood, just like a struct with 2 int fields. For that manner, a lot of generic programming would get simpler if tupleof worked with the built-in D types.

(Of course we could just hack it via static if statements)


More information about the Digitalmars-d mailing list