Bug 2659 – Remove the comma operator

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2009-02-13T10:06:00Z
Last change time
2017-08-02T08:07:14Z
Assigned to
nobody
Creator
clugdbug

Comments

Comment #0 by clugdbug — 2009-02-13T10:06:09Z
The comma operator is an unintuitive operator with very few commendable uses. If comma were added to the increment grammar of the for loop, the comma operator could be removed from the language. It's another piece of baggage from C.
Comment #1 by wbaxter — 2009-02-16T19:10:34Z
(In reply to comment #0) > The comma operator is an unintuitive operator with very few commendable uses. About the only real use it gets in C is in preprocessor macro hacks. And we all know D's stance on preprocessing. > If comma were added to the increment grammar of the for loop, > the comma operator could be removed from the language. Or put to better purpose making (a,b,c) a literal tuple syntax.
Comment #2 by clugdbug — 2012-10-23T07:45:53Z
Just encountered another newbie reason for removing comma. writeln( 6, mixin("7,8"), 9 ); doesn't print the expected 6 7 8 9. Instead it prints 6 8 9. If comma was removed, this wouldn't compile.
Comment #3 by yebblies — 2014-02-05T04:46:48Z
Comment #4 by shorttail — 2014-02-28T01:07:10Z
vec = overloaded vector type of size 3. What I meant to write: vec v = vec(0, 0, 3); What I actually wrote: vec v = (0, 0, 3); Result: vec v = 3; // as in vec(3, 3, 3); It was quite nasty. Considering D doesn't allow statements like 3; I don't see why it isn't an error to do it when using the comma operator in an assignment. It literally just throws away everything with no side effect. Or make it easy and just remove the comma operator completely in its current form.
Comment #5 by yebblies — 2014-02-28T01:13:00Z
(In reply to comment #4) > vec = overloaded vector type of size 3. > > What I meant to write: > vec v = vec(0, 0, 3); > > What I actually wrote: > vec v = (0, 0, 3); > > Result: > vec v = 3; // as in vec(3, 3, 3); > > It was quite nasty. Considering D doesn't allow statements like 3; I don't see > why it isn't an error to do it when using the comma operator in an assignment. > It literally just throws away everything with no side effect. > There was an attempt in the past to error on a commaexp where the non-last expression has no side effects, but it was never enabled because the compiler generates thousands of these, screwing everything up. It will probably be added eventually. > Or make it easy and just remove the comma operator completely in its current > form. That's the plan, hopefully in 2.066.
Comment #6 by devw0rp — 2014-02-28T01:18:35Z
+1 to this. Also, I wouldn't use a comma in future for tuple syntax with parentheses. I think that was a mistake in Python. Supposing this was translated to D as it is in Python... auto x = (); // empty tuple auto x = (1); // value 1 auto x = (1,) // tuple of size 1 auto x = (1, 2) // pair After so many years, I still make mistakes regularly with this.
Comment #7 by bearophile_hugs — 2014-03-10T14:38:03Z
The reduction of a real bug: bool foo(int x, int* y=null) { return true; } void main() { int x; int* p; if (foo(x), p) { assert(0, "true branch"); } else { assert(0, "false branch"); } } Hopefully the comma operator will be deprecated.
Comment #8 by temtaime — 2014-03-11T06:22:09Z
In some cases comma operator is useful. I disagree with that we need to deprecate it.
Comment #9 by aliloko — 2014-03-11T06:27:38Z
There is evidence comma operators actively creates bugs: In article: http://www.viva64.com/en/b/0240/ Bugs: http://www.viva64.com/en/b/0240/#ID0EXCAI http://www.viva64.com/en/b/0240/#ID0E4RAI
Comment #10 by bearophile_hugs — 2014-03-11T07:06:06Z
(In reply to comment #9) > There is evidence comma operators actively creates bugs: > In article: http://www.viva64.com/en/b/0240/ > Bugs: http://www.viva64.com/en/b/0240/#ID0EXCAI > http://www.viva64.com/en/b/0240/#ID0E4RAI The problems caused by the comma operator are more wider than that.
Comment #11 by bearophile_hugs — 2014-03-11T07:12:54Z
(In reply to comment #8) > In some cases comma operator is useful. > I disagree with that we need to deprecate it. As usual you have to weight all the advantages against all the disadvantages and then decide. From my experience in coding in C/D the usages of the comma operator are good when formalized (like inside variable definitions, in function arguments, etc), but in free/wild situations it's too much risky (and using it for tuples some years from now is a better use). So please try to list every one of your useful usages of the comma operator, and let's see.
Comment #12 by mathias.lang — 2016-05-07T13:09:51Z
Comment #13 by github-bugzilla — 2016-06-10T03:29:06Z
Commits pushed to master at https://github.com/dlang/dmd https://github.com/dlang/dmd/commit/058c5a301fb3942e01446a60381d5e935ab91ec7 Fix Issue 2659 - Deprecate using the result of comma expression This should weed out most of the bugs. https://github.com/dlang/dmd/commit/0a7f87a17304c067ad42e5a25cce36c920d5b467 Merge pull request #5737 from mathias-lang-sociomantic/coma-operator-must-die Fix Issue 2659 - Deprecate using the return of a comma expression
Comment #14 by github-bugzilla — 2016-10-01T11:47:34Z
Commits pushed to stable at https://github.com/dlang/dmd https://github.com/dlang/dmd/commit/058c5a301fb3942e01446a60381d5e935ab91ec7 Fix Issue 2659 - Deprecate using the result of comma expression https://github.com/dlang/dmd/commit/0a7f87a17304c067ad42e5a25cce36c920d5b467 Merge pull request #5737 from mathias-lang-sociomantic/coma-operator-must-die
Comment #15 by github-bugzilla — 2017-08-02T08:07:14Z
Commit pushed to dmd-cxx at https://github.com/dlang/dmd https://github.com/dlang/dmd/commit/9232e99814037727a42e0b1a646a908bde68ed40 Issue 2659 - Deprecate using the result of comma expression