Should this be flagged as a warning?

Ben Davis entheh at cantab.net
Wed Sep 26 15:21:36 PDT 2012


On 26/09/2012 10:45, Don Clugston wrote:
> On 25/09/12 21:30, Bernard Helyer wrote:
>> I tried to post this last night, but the NG wasn't having any of it.
>>
>> I found myself writing a bug that looked like this
>>
>>      match(ts, TokenType.Is);
>>      match(ts, TokenType.OpenParen);
>>      isExp.type == parseType(ts);
>>
>> The bug being of course, that a type is parsed and ts is modified,
>> so the expression has side effects so it's not flagged as a useless
>> expression. But the comparison still has no effect, so should this be
>> flagged by DMD?
>>
>> -Bernard.
>
>
> The "must have an effect" rule only applies to statements, not
> expressions, so this is according to the spec. It's not a bug.
>
> This is a bit like the more extreme case I recently posted about:
>
> int x, y, z;
> x == y, ++z;
>
> doesn't generate an error message even though x == y has no
> side-effects, because comma is an expression, not a statement.
>
> IMHO, every expression should be required to have an effect. For example
> foo() + foo();
> shouldn't compile, unless + is an overloaded operator with side-effects.

What's a realistic use case for allowing foo() + foo(), even if it is 
overloaded to have side-effects? It looks like an expression with no 
side-effects. Surely if anyone actually intends it to have side-effects, 
then they've made a fundamental design mistake in their API?

I know C++ has << used this way, but that's a bit horrible.


More information about the Digitalmars-d mailing list