File tree Expand file tree Collapse file tree 1 file changed +21
-3
lines changed Expand file tree Collapse file tree 1 file changed +21
-3
lines changed Original file line number Diff line number Diff line change @@ -300,11 +300,29 @@ $(GNAME CommaExpression):
300
300
)
301
301
302
302
$(P The left operand of the $(D ,) is evaluated, then the right operand
303
- is evaluated. The type of the expression is the type of the right
304
- operand, and the result is the result of the right operand.
305
- Using the result of comma expressions isn't allowed.
303
+ is evaluated.
304
+ In C, the result of a comma expression is the result of the right operand.
305
+ In D, using the result of a comma expression isn't allowed.
306
+ Consequently a comma expression is only useful when each operand has
307
+ a side effect.
306
308
)
307
309
310
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
311
+ ---
312
+ int x, y;
313
+ // expression statement
314
+ x = 1, y = 1;
315
+ // evaluate a comma expression at the end of each loop iteration
316
+ for (; y < 10; x++, y *= 2)
317
+ writefln("%s, %s", x, y);
318
+ ---
319
+ )
320
+ $(RATIONALE The comma expression has been used unintentionally, either by
321
+ bracket nesting mistakes or when users expect a sequence of arguments instead
322
+ of a single expression. Those bugs can be hard to detect in code review.
323
+ Disallowing use of the result turns those bugs into errors.)
324
+
325
+
308
326
$(H2 $(LNAME2 assign_expressions, Assign Expressions))
309
327
310
328
$(GRAMMAR
You can’t perform that action at this time.
0 commit comments