If Statement with Declaration

Anonymouse via Digitalmars-d digitalmars-d at puremagic.com
Mon Nov 7 08:18:22 PST 2016


On Sunday, 6 November 2016 at 05:07:10 UTC, Andrei Alexandrescu 
wrote:
> // possible future D
> if ((auto variable = fun()) != 42) {
>   ...
> }
>
> Defining a variable in an expression wouldn't be allowed 
> everywhere (but might be contemplated later as an possibility, 
> which is a nice thing about this syntax).

I like it but it would have to require the parantheses, or you 
could get ambiguities like:

if (auto variable = noParensGetSomeT == true) {
     // is variable of type T or bool, if T can be implicitly cast?
}

> A more approachable thing to do is allow variable definitions 
> in switch statements:
>
> switch (auto x = fun() { ... }
>
> It is surprising that doesn't work, which is a good argument in 
> favor of the feature (removal of an undue limitation, rule of 
> least astonishment etc).

This I can get behind, would start using it right away.

> Andrei


More information about the Digitalmars-d mailing list