Multiple return values...
Robert Jacques
sandford at jhu.edu
Sat Mar 10 07:07:06 PST 2012
On Sat, 10 Mar 2012 00:30:05 -0600, Andrei Alexandrescu <SeeWebsiteForEmail at erdani.org> wrote:
> On 3/9/12 9:35 PM, Robert Jacques wrote:
>> On Fri, 09 Mar 2012 22:16:44 -0600, Andrei Alexandrescu
>> <SeeWebsiteForEmail at erdani.org> wrote:
>>> On 3/9/12 3:59 PM, Timon Gehr wrote:
>>>> Notably, there is no convenient unpacking syntax. Walter does not merge
>>>> the patches of Kenji Hara that would fix this because presumably he
>>>> fears it could get in the way of a more general solution.
>>>
>>> This is because we need to think about that stuff to maximize its
>>> generality.
>>
>> Was simply applying the unpacking rules to all types (i.e. structs,
>> classes, etc) ever considered?
>
> How would that work?
>
> Andrei
>
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];
That way, fun() could return any struct / class, not just a Tuple. For example,
float3 position = { 1.0f, 2.0f, 3.0f };
auto (x1) = position;
auto (x2,y2) = position;
auto (x3,y3,z3) = position;
assert(x1 == x2 && x1 == x3 && x1 == 1);
assert(y2 == y3 && y2 == 2);
assert(z3 == 3);
would be valid. Note that only unpacking a few of the components of a MRV function is a common practice in my experience (mostly Matlab).
P.S. For unpacking classes / structs directly, i.e.
auto (x3,y3,z3) = position;
should lower to
auto x3 = position.tupleof[0];
auto y3 = position.tupleof[1];
auto z3 = position.tupleof[2];
in order to avoid the extra temporary.
P.S.S. Unpacking should respect protection attributes. Since tupleof doesn't respect private, protected, etc, the actual lowering should be:
auto __tup = fun();
double x = mixin(__tup.tupleof[0].stringof);
int y = mixin(__tup.tupleof[1].stringof);
More information about the Digitalmars-d
mailing list