Nitpicking the D grammar
Jascha Wetzel
"[firstname]" at mainia.de
Fri Sep 7 06:37:21 PDT 2007
Declaration vs. ExpressionStatement
struct S
{
void opAdd(S s) {}
void opMul(S s) {}
}
void main()
{
S a, b;
a * b; // Error: a is used as a type
a + b; // compiles fine
}
The specs state that an expression is disallowed as a statement if it
has no effect. DMD consequently allows expressions like "a + b" if they
invoke an overloaded operator, except, and this is my point here, if the
expression can be parsed as a declaration. In that case DMD prefers the
declaration.
Disallowing expressions without an effect is nice, but wouldn't a
warning suffice? After all the compiler can't find all expressions that
have no effect. Besides, adding for example two integers and not saving
the result (which is classified as a no-effect-expression) still has the
effect of changing the eflags register, which may be read in an asm
block. So it's questionable what "no effect" actually means.
Wouldn't it be more appropriate here to change the specs to
"Expressions that can be interpreted as a declaration, like (a*b), are
illegal in expression statements. If such an expression is needed,
casting it to void will make it legal."
(http://www.digitalmars.com/d/statement.html#ExpressionStatement)
and change the no-effect-error into a warning?
More information about the Digitalmars-d
mailing list