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.
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 #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