Should we deprecate comma?

Adam D. Ruppe destructionator at gmail.com
Sun Mar 23 19:50:17 PDT 2014


On Monday, 24 March 2014 at 02:31:46 UTC, Andrei Alexandrescu 
wrote:
> One concession we could make would be to disallow using the 
> result of the operator. That might actually catch all bugs 
> discussed herein.

If we go that far, we might as well just kill the whole thing, 
since half* the reason of using an expression in the first place 
is to use the result. Otherwise, you might as well use a 
statement.

if (condition) ++i, ++j; // might as well be { ++i; ++j; }

* The other half of the uses being where you want a statement, 
but the grammar won't allow it... but even in those cases, the 
ONLY time I can think of where this is the case AND you don't 
need the result is the increment expression of the for loop.

So with that compromise, it is equivalent to banning the comma 
expression except in the special case of the for loop and in code 
that has no real reason to use it in the first place (and is also 
the most likely place where it was used unintentionally).

** Concrete example of where I (/very/ rarely apparently) 
consciously use the comma operator:

int a = something == 1 ? 1
       : something == 2 ? 2
       : (assert(0), 0);

There, I want the assert(0) to trigger in the default case, but 
the ternary operator still needs a value to assign to the 
variable a, so the ,0 provides that. Never actually used, but 
required for the common type of the ternary to match up.


More information about the Digitalmars-d mailing list