If Statement with Declaration

Nick Treleaven via Digitalmars-d digitalmars-d at puremagic.com
Mon Nov 7 03:10:38 PST 2016


On Sunday, 6 November 2016 at 05:07:10 UTC, Andrei Alexandrescu 
wrote:
> if (auto variable = fun(); variable != 42) {
>   ...
> }
>
> Why does the word "variable" need to appear twice? It seems 
> simpler to allow punctuation around existing syntax:
>
> // possible future D
> if ((auto variable = fun()) != 42) {
>   ...
> }

Avoiding stuttering is nice, but maybe it could be made a bit 
clearer if declarations were allowed for ref arguments and we 
used a named function:

alias let = (ref v) => v;

if (let(auto variable = fun()) != 42) {...}

The same feature allows tuple unpacking:

produceTuple.unpack(auto str, int i);

C# has argument declarations. Just an idea. I personally like the 
C++ syntax too as local variable names are usually short and it 
avoids brackets.


More information about the Digitalmars-d mailing list