Comma operator = broken design

Robert Jacques sandford at jhu.edu
Wed Dec 7 09:32:54 PST 2011


On Wed, 07 Dec 2011 11:49:33 -0500, Alex Rønne Petersen <xtzgzorex at gmail.com> wrote:
> Hi,
>
> Consider this code:
>
> if (condition)
>      foo();
> else
>      bar(),
>
> baz();
>
> Notice the comma in the bar call. This will actually compile. Why?
> Because the program is really interpreted as:
>
> if (condition)
> {
>      foo();
> }
> else
> {
>      bar();
>      baz();
> }
>
> This is, honestly, ridiculous. On most European keyboard layouts, comma
> is on the same key as semicolon. This means that a typo such as the
> above can lead to an incorrect (but compiling) program easily, rather
> than a compile-time error.
>
> I really do not see the value in allowing such syntax in the first
> place. I've been told that one argument was that generated code might
> use it, but I have no idea why it would be needed. Furthermore, this
> operator makes it very hard to introduce Python-style tuples in the
> language.
>
> Why is this operator still kept around?

Take your pick:
1) So that legacy B/C/C++/D1/etc code can be trivially ported to D1/D2.
2) The comma operator is heavily used in regular old for loops.
3) It is frequently used internally by the compiler to effect syntactic lowerings. A decent portion of D's syntax is not transformed directly to instructions; instead it is converted to simpler code which is in turn processed. Technically the compiler doesn't need ',' in the language to do this.
4) D has a policy of 'If C/C++ code compiles in D without error, it must have the same syntactic meaning as in C.' So even if we remove the comma operator, we might not get comma-style tuples. (I don't know if anyone has looked at this issue in depth)
5) The comma and semicolon are on different keys on US style keyboards.
6) It is trivial to change your keyboard layout (I've hotkeyed Greek for fast math symbols)
7) We are trying to stabilize the language, so massive code-breaks are frowned upon.

Personally, I think real tuples outweigh the comma-operator, but real tuples don't necessarily need the ',' (we could use '..' or something else instead) and removing comma-expressions isn't without its problems.

P.S. I was going to suggest testing/looking into issue 4, but a quick test in D suggests that there are valid ',' and '(,)' style expressions in C/C++ that would remain valid in D with comma-tuples.


More information about the Digitalmars-d mailing list