Multiple return values...

Iain Buclaw ibuclaw at ubuntu.com
Fri Mar 16 01:48:41 PDT 2012


On 16 March 2012 02:26, Andrei Alexandrescu
<SeeWebsiteForEmail at erdani.org> wrote:
> On 3/15/12 5:44 PM, foobar wrote:
>>
>> On Thursday, 15 March 2012 at 18:23:57 UTC, Andrei Alexandrescu wrote:
>>>
>>>
>>> I understand how the draw of reaching for new syntax is extremely
>>> alluring. I used to fall for it much more often, but over years I have
>>> hardened myself to resist it, and I think that made me a better
>>> language designer. In this stage of D, I think we should all have an
>>> understanding that adding syntax is not a win. Instead, it is an
>>> acknowledgment that the existing language, for all its might, is
>>> unable to express the desired abstraction. This is sometimes fine
>>> (such as in the case of introducing multiple new symbols from one
>>> tuple), but generally the question is not "why couldn't we add syntax
>>> X?" but instead "why should we add syntax X?"
>>>
>>>
>>> Andrei
>>
>>
>> I agree that this is an acknowledgement of the current language's
>> inability to express the abstraction. That's why many people ask for it
>> to be added in the first place. We should add this syntax because it
>> *is* impossible ATM to implement the required abstraction.
>
>
> I think this is a reasonable request:
>
> (auto a, b) = fun();
>
> --->
>
> static assert(fun().length == 2);
> auto __t = fun();
> auto a = __t[0];
> auto b = __t[1];
>
> To express the idiom in the example, the programmer needs to do the
> expansion by hand, which is verbose and introduces unnecessary symbols. So
> the language as is has an expressiveness problem (albeit not a pernicious
> one) that is nice to solve.
>

Andrei++

This could also be done for arrays too.

(int a, b) = arr[];

----->

static assert(arr.length == 2);
int a = arr[0];
int b = arr[1];



Or possibly a use in variadic templates, which could make D act more
like how some scripting languages work.

void conn(T ...)(T args) {
  // vars get default init if value wasn't passed to function.
  (string server, port, username, password) = args[];
}




-- 
Iain Buclaw

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


More information about the Digitalmars-d mailing list