If Statement with Declaration
Nick Treleaven via Digitalmars-d
digitalmars-d at puremagic.com
Mon Jul 24 06:12:17 PDT 2017
On Friday, 21 July 2017 at 21:50:02 UTC, Johan Engelen wrote:
> We do have a construct like that already:
> ```
> static if (is(E V == enum)) { // this inserts "V"
> V v;
> }
> ```
Yes, but this is a bit different as the goal is to avoid
repeating V (as it may be complex). Repeating a local variable
name (using e.g. `with...if`) isn't a problem as the variable
should have a short identifier. With the above `is` expression
it's unavoidable to have a test and a declaration combined. That
said, the syntax could be much clearer:
static if (is(E == enum; alias V)) { // this inserts "V"
V v;
}
This syntax clearly separates the test from the declaration, and
makes the declaration obvious due to the alias keyword. The test
comes first as it logically should, as the alias is not being
used until the following statement. It extends to the other `is`
expression forms:
is(T; alias A)
is(T : U; alias A)
is(T : U!V, U, V; alias A)
is(T == U!V, U, V; alias A)
(http://dlang.org/spec/expression.html#IsExpression)
Whenever I want to use the identifier variant I always end up
checking the spec. When I see it in code I usually have to stop
what I was thinking about to parse the `is` expression.
More information about the Digitalmars-d
mailing list