Skip to content

Commit e728b06

Browse files
ntreldlang-bot
authored andcommitted
[spec/expression] Tweak Order of Evaluation
Add 2 paragraph tags. Add subheadings and move best practice/implementation defined notes to relevant places. Add example for prefix/postfix increment. Use ordered list for function call evaluation.
1 parent 486d167 commit e728b06

File tree

1 file changed

+38
-13
lines changed

1 file changed

+38
-13
lines changed

spec/expression.dd

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,21 @@ $(GLINK OrOrExpression) (`||`), such that $(I expr) is a subexpression of $(I sc
9696
---
9797
((f() * 2 && g()) + 1) || h()
9898
---
99-
The smallest short-circuit expression
100-
of the subexpression `f() * 2` above is `f() * 2 && g()`. Example:
99+
$(P The smallest short-circuit expression
100+
of the subexpression `f() * 2` above is `f() * 2 && g()`. Example:)
101101
---
102102
(f() && g()) + h()
103103
---
104-
The subexpression `h()` above has no smallest short-circuit expression.
104+
$(P The subexpression `h()` above has no smallest short-circuit expression.)
105+
105106

106107
$(H2 $(LNAME2 order-of-evaluation, Order Of Evaluation))
107108

109+
$(BEST_PRACTICE Even when the order of evaluation is well-defined, writing code that
110+
depends on it is rarely recommended.)
111+
112+
$(H3 $(LNAME2 order-increment, Increment and Decrement))
113+
108114
$(P Built-in prefix unary expressions `++` and `--` are evaluated as if lowered (rewritten) to
109115
$(RELATIVE_LINK2 assignment_operator_expressions, assignments) as follows:)
110116

@@ -129,6 +135,21 @@ $(TABLE
129135
$(P Therefore, the result of postfix
130136
`++` and `--` is an rvalue just before the side effect has been effected.)
131137

138+
$(SPEC_RUNNABLE_EXAMPLE_RUN
139+
---
140+
int i = 0;
141+
assert(++i == 1);
142+
assert(i++ == 1);
143+
assert(i == 2);
144+
145+
int* p = [1, 2].ptr;
146+
assert(*p++ == 1);
147+
assert(*p == 2);
148+
---
149+
)
150+
151+
$(H3 $(LNAME2 order-binary, Binary Expressions))
152+
132153
$(P Binary expressions except for $(GLINK AssignExpression), $(GLINK OrOrExpression), and
133154
$(GLINK AndAndExpression) are evaluated in lexical order (left-to-right). Example:)
134155

@@ -145,14 +166,24 @@ first. Then, $(GLINK OrOrExpression) evaluates its right-hand side if and only i
145166
side does not evaluate to nonzero. $(GLINK AndAndExpression) evaluates its right-hand side if and
146167
only if its left-hand side evaluates to nonzero.)
147168

169+
$(IMPLEMENTATION_DEFINED The order of evaluation of the operands of $(GLINK AssignExpression).)
170+
171+
$(H3 $(LNAME2 order-conditional, Conditional Expressions))
172+
148173
$(P $(GLINK ConditionalExpression) evaluates its left-hand side argument
149174
first. Then, if the result is nonzero, the second operand is evaluated. Otherwise, the third operand
150175
is evaluated.)
151176

177+
$(H3 $(LNAME2 order-calls, Function Calls))
178+
152179
$(P Calls to functions with `extern(D)` $(DDSUBLINK spec/attribute, linkage, linkage) (which is
153-
the default linkage) are evaluated in the following order: first, if necessary, the address of the
154-
function to call is evaluated (e.g. in the case of a computed function pointer or delegate). Then,
155-
arguments are evaluated left to right. Finally, transfer is passed to the function. Example:)
180+
the default linkage) are evaluated in the following order:)
181+
1. If necessary, the address of the
182+
function to call is evaluated (e.g. in the case of a computed function pointer or delegate).
183+
1. Arguments are evaluated left to right.
184+
1. Transfer of execution is passed to the function.
185+
186+
$(P Example calling a $(DDSUBLINK spec/function, function-pointers, function pointer):)
156187

157188
$(SPEC_RUNNABLE_EXAMPLE_RUN
158189
---
@@ -173,14 +204,8 @@ fun()(f1(), f3(f2()), f4());
173204
---
174205
)
175206

176-
$(IMPLEMENTATION_DEFINED
177-
$(OL
178-
$(LI The order of evaluation of the operands of $(GLINK AssignExpression).)
179-
$(LI The order of evaluation of function arguments for functions with linkage other than `extern (D)`.)
180-
))
207+
$(IMPLEMENTATION_DEFINED The order of evaluation of function arguments for functions with linkage other than `extern(D)`.)
181208

182-
$(BEST_PRACTICE Even though the order of evaluation is well-defined, writing code that
183-
depends on it is rarely recommended.)
184209

185210
$(H2 $(LNAME2 temporary-lifetime, Lifetime of Temporaries))
186211

0 commit comments

Comments
 (0)