If Statement with Declaration

Enamex via Digitalmars-d digitalmars-d at puremagic.com
Sun Nov 6 06:16:48 PST 2016


On Sunday, 6 November 2016 at 05:07:10 UTC, Andrei Alexandrescu 
wrote:
>
> The declaration with "if" seems to be a recent fashion. I've 
> first seen it in Go and now C++17 took a shine to it - 
> http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0305r0.html. A DIP would do good to cite that related work.
>
> It seems a low impact feature. Also, the Go/C++ syntaxes seem 
> suboptimal to me because they are stuttering:
>
> if variable := fun(); variable != 42 {
>   ...
> }
>
> or (C++):
>
> 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) {
>   ...
> }
>
> 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).
>
> Andrei

I remember an old suggestion/DIP allowing 'with' statements to 
introduce/declare symbols/variables.

Might be a cleaner extension to existing language:

with(auto x = f()) if(foo(x)) {}
else with(auto r = root(t)) if(leaf(r) || blah < bar) {}


More information about the Digitalmars-d mailing list