-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
clarification on semantics of ??= #11
Comments
I raised this question with Gilad in March, and I believe that is what prompted him to change the semantics from For context, here's the line of reasoning that motivated me to suggest the change:
|
Thanks for the detailed answer Paul - and thanks for the link to the spec changes! I was only aware of the proposal in this repo, I didn't know the changes were already added to the actual spec, I'll take a look there as well. Regarding:
Not sure I follow this, given that ((x) => x == null ? null : x.b = exp)(a) It seems that desugarizing ((x) => x == null ? null : x.b = a?.b ?? c)(a) |
Follow up question: how many times do we print var _x = new A();
get x {
print('read');
return _x;
}
main() {
x?.y ??= 3;
} It seems like under the old semantics we'd print it twice: x?.y ??= 3
// is equivalent to:
x?.y = x?.y ?? 3
// which corresponds to:
t1 = x; // (x in the lhs, prints "read")
if (t1 != null) {
t2 = x; // (x in the rhs, prints "read")
if (t2 != null) {
t3 = t2.y;
}
if (t3 == null) t3 = 3;
t1.y = t3;
} It appears that under the new semantics we print it 3 times: x?.y ??= 3
// expands to:
t1 = x?.y; // prints "read"
if (t1 == null) {
x?.y = x?.y ?? 3 // prints read twice like above.
} is that correct? |
Regarding Regarding the question of how many times "read" should be printed in the expression
(to avoid confusion I used In general all the null-aware operators should evaluate subexpressions once if necessary, and zero times otherwise. Otherwise subexpressions with side effects will behave unexpectedly. Consider that a user might want to do |
Sorry I didn't point you to the spec changes earlier. I've updated the tracking bugs dartbug.com/23454 and dartbug.com/23455 to point to the spec changes so that hopefully Matthias won't fall into the same trap. |
Thanks Paul! |
Question for @gbracha and @stereotype441
The proposal says that the semantics of
a ??= b
are the same as:This means,
a
will be assigned it's own value when it's not null. This can be observable if you do:However, some tests are written with a different semantics that avoids the assignment if the value is not null. In other words:
I currently have the former implemented in dart2js and I'm inclined to change those tests to match, but I wanted to verify this was the intended behavior.
The text was updated successfully, but these errors were encountered: