If Statement with Declaration
Andrei Alexandrescu via Digitalmars-d
digitalmars-d at puremagic.com
Sat Nov 5 22:07:10 PDT 2016
On 11/5/16 3:52 PM, Jonathan M Davis via Digitalmars-d wrote:
> Ultimately though, the biggest hurdle is that someone needs to create a DIP
> for it that strongly argues its case with real world examples of how it
> would improve code (preferably with code from Phobos and code.dlang.org),
> and without a really well-written DIP it's going to be dead in the water
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).
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).
Andrei
More information about the Digitalmars-d
mailing list