Should the comma operator be removed in D2?

Lutger lutger.blijdestijn at gmail.com
Mon Nov 16 15:53:52 PST 2009


Jason House wrote:

> Lutger Wrote:
> 
>> dsimcha wrote:
>> 
>> > == Quote from Justin Johansson (no at spam.com)'s article
>> >> Mentioned in the switch case statements thread, this probably should
>> >> be a separate discussion thread.
>> >> Is the comma operator "another piece of C cruft" that needs to be
>> >> removed from D(2)?
>> >> grauzone wrote:
>> >>  > Justin Johansson wrote:
>> >>  >> bearophile wrote:
>> >>  >>> What's bad about forcing people to write:
>> >>  >>> case A, B, C:
>> >>  >>>
>> >>  >>> Instead of:
>> >>  >>> case A:
>> >>  >>> case B:
>> >>  >>> case C:
>> >>  >>> ?
>> >>  >>>
>> >>  >>> Bye,
>> >>  >>> bearophile
>> >>  >>
>> >>  >> I don't know about "forcing people" to write such but perhaps it
>> >>  >> could be an "option for people" :-)
>> >>  >>
>> >>  >> Actually I quite like the brevity you propose but would it be a
>> >>  >> challenge for the comma operator?
>> >>  >>
>> >>  >> While ago, I got D multi-dimensional array syntax messed up and
>> >>  >> declared such an animal as
>> >>  >> int[3,4,5] which effectively ended up declaring the beast as
>> >>  >> int[5].
>> >>  >
>> >>  > The comma operator is another piece of C cruft that needs to go.
>> >>  >
>> >>  >> Cheers Justin
>> > 
>> > Can someone please explain to me what the comma operator does?  I've
>> > seen this
>> > mentioned here before, but I really don't know.  Then again, if the
>> > only people who use it are crufty old C language lawyers and people who
>> > have never programmed seriously in C before don't know about it, I
>> > guess that's an indicator that it's not worth much.
>> 
>> It is very simple, evaluates the expression before the comma and after
>> the comma. The result is the value of the expression after the comma.
>> 
>> int a = 1;
>> int b = --a, ++a;
>> assert(b == 1);
>> assert(a == 1);
> 
> I'm no language lawyer, but I'm pretty sure that's not right. I don't
> think there's an order of evaluation rules. b could just as easilly end up
> with a value of 2.

No I think it's right, also in C++:

http://www.digitalmars.com/d/2.0/expression.html
http://en.wikipedia.org/wiki/Sequence_point



More information about the Digitalmars-d mailing list