Does D have an equvalent of: if (auto = expr; expr)

Sebastiaan Koppe mail at skoppe.eu
Fri Feb 7 10:36:41 UTC 2020


On Friday, 7 February 2020 at 08:52:44 UTC, mark wrote:
> Some languages support this kind of thing:
>
> if ((var x = expression) > 50)
>   print(x, " is > 50")
>
> Is there anything similar in D?

Yes and no.

It only works for bools or things that convert to bool.

You might have seen:

string[string] dict;
if (auto p = "key" in dict) {
   writeln(*p);
}

That works because the 'in' operator on aa's returns a pointer, 
which can be compared to bool.


More information about the Digitalmars-d-learn mailing list