Skip to content

Commit 1c2c7cb

Browse files
authored
[spec/expression] Update CommaExpression docs (#3896)
`typeof(expr, expr)` is an error, so remove sentence about result type. Add example. Add rationale for disallowing use of result.
1 parent af57ad4 commit 1c2c7cb

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

spec/expression.dd

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,11 +300,29 @@ $(GNAME CommaExpression):
300300
)
301301

302302
$(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.
306308
)
307309

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+
308326
$(H2 $(LNAME2 assign_expressions, Assign Expressions))
309327

310328
$(GRAMMAR

0 commit comments

Comments
 (0)