From b04771e78f9bf9acb5ef0cfcae6d9fa6c44ebfcd Mon Sep 17 00:00:00 2001 From: Johan Engelen Date: Tue, 12 Jul 2016 11:14:47 +0200 Subject: [PATCH] Clarify spec for "+=" and friends See https://forum.dlang.org/post/mxvdieydnicvdxbcklrd@forum.dlang.org (submitted through the website and I cannot make changes to this PR. Because I am cleaning up after people, if you don't like it, please submit your own PR.) --- spec/expression.dd | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/spec/expression.dd b/spec/expression.dd index 8f6c62d8cd..c0ccaa9cb0 100644 --- a/spec/expression.dd +++ b/spec/expression.dd @@ -96,10 +96,38 @@ $(H4 Assignment Operator Expressions) $(P except that:) $(UL - $(LI operand $(D a) is only evaluated once) + $(LI operand $(D a) is only evaluated once, see below.) $(LI overloading $(I op) uses a different function than overloading $(I op)= does) $(LI the left operand of $(D >>>=) does not undergo integral promotions before shifting) ) + + $(P The expression evaluates according to + + -------------- + void doOpAssignment(ref int x, int y) + { + x = x op y; + } + doOpAssignment(a, b); + -------------- + + thus the following unittest passes + + -------------- + int sum; + int add7tosum_return1() { sum += 7; return 1; } + unittest + { + sum = 0; + sum += add7tosum_return1(); + assert(sum == 8); // "eight" ! + + sum = 0; + sum = cast(typeof(sum))(sum + add7tosum_return1()); + assert(sum == 1); // "one" ! + } + -------------- + ) $(H3 Conditional Expressions)