Evaluation order of "+="
deadalnix via Digitalmars-d
digitalmars-d at puremagic.com
Mon Jul 11 19:27:04 PDT 2016
On Monday, 11 July 2016 at 23:04:00 UTC, Johan Engelen wrote:
> LDC recently changed the evaluation order of "+=" (I think
> unintentionally, some other eval order problems were fixed).
> Now, it is different from DMD.
> I am going to argue that I think DMD's order is more useful in
> the context of fibers, and would like your opinion.
>
> Consider this code:
> ```
> int sum;
>
> int return1_add9tosum() {
> sum += 9;
> return 1;
> }
>
> void main() {
> sum = 0;
> sum += return1_add9tosum();
>
> import std.stdio;
> writeln(sum);
> }
> ```
> DMD 2.071 prints "10".
> LDC master prints "1". (LDC 1.0.0 prints "10")
>
> I find the spec [1] to be unclear on this point, so which one
> is correct?
>
> The bug was caught by code involving fibers. Instead of
> `return1_add9tosum`, a function `return1_yieldsFiber` is
> called, and multiple fibers write to `sum`. In that case, upon
> completing the "+=", an older version of `sum` is used to
> calculate the result. I think that it is best to do what DMD
> does, such that fibers can do "+=" without worrying about
> yields on the rhs.
>
> [1] https://dlang.org/spec/expression.html
There was a very lenghty discussion about this in the past. DMD
is correct on that one. The semantic is such as :
int plusEqual(ref int a, int b) {
a = a + b;
return a;
}
More information about the Digitalmars-d
mailing list