Evaluation order of "+="
Johan Engelen via Digitalmars-d
digitalmars-d at puremagic.com
Mon Jul 11 16:04:00 PDT 2016
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
More information about the Digitalmars-d
mailing list