Killing the comma operator

Mathias Lang via Digitalmars-d digitalmars-d at puremagic.com
Tue May 10 02:52:07 PDT 2016


If you were attending DConf2016, or watched Ali (excellent) talk 
[1] at some point, you probably remember this piece of code:

`synchronized (lockA, lockB)`

As documented in TDPL (thanks @schveiguy) [2].
Unfortunately, as someone pointed out, this isn't implemented, 
but still compile "thanks" to the comma operator [3].

There are many reasons to hate this almost invisible false friend.
One more example is returning a string containing a comma to mix 
it in. If you're not familiar enough with the language, you'll 
end up wondering why it isn't behaving as expected [4].
But there can be situations where it's useful. For example the 
following code isn't rare:

`for (; !a.empty && !b.empty; a.popFront, b.popFront) { /* Do 
stuff with ranges */ }`

Rewriting this wouldn't look as nice.

So, following DConf2016, I raised a P.R. to deprecate usage of 
the comma expressions, except within `for` loops increment [5].
The feedback we already got from druntime / phobos was pretty 
good: out of three occurrences in druntime, one was a bug [7]. 
Not a single message was triggered in Phobos, since it's using 
Dscanner, which is warning against it in assignments and 
(nowadays) synchronized [6].

It seems there is a reasonable ground to kill it. However, there 
have been legitimated concern about code breakage, so we would 
like to hear from other people:

Do you like comma expressions, and think its presence in the 
language is more pro than con ?


[1]: http://www.ustream.tv/recorded/86352137/highlight/699197
[2]: 
http://www.informit.com/articles/article.aspx?p=1609144&seqNum=15
[3]: 
https://github.com/dlang/dmd/blob/81cb1c8a6a4d594bd17bfc8e263b72ed72ff8cf3/src/parse.d#L5662-L5683
[4]: https://issues.dlang.org/show_bug.cgi?id=15993
[5]: https://github.com/dlang/dmd/pull/5737
[6]: 
https://github.com/Hackerpilot/Dscanner/blob/02f8f3c423a413d0f90e9d4698a2db9049e35b44/src/analysis/comma_expression.d
[7]: https://github.com/dlang/druntime/pull/1562


More information about the Digitalmars-d mailing list