Skip to content
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

semantics of assigning to a?.x #9

Open
sigmundch opened this issue May 16, 2015 · 4 comments
Open

semantics of assigning to a?.x #9

sigmundch opened this issue May 16, 2015 · 4 comments

Comments

@sigmundch
Copy link

What should we expect with the ?. operator when used with assignments:

exp1?.x = exp2

My current expectation is that this should be equivalent to:

((a, b) => a == null ? null : a.x = b) (exp1, exp2);

(both expressions are evaluated once, exp2 is evaluated even when exp1 is null)

@nex3
Copy link

nex3 commented May 16, 2015

Since set x is just a method, it should presumably do the same thing as exp1?.x(exp2) with regards to whether exp2 is evaluated or not. I'm not sure what that same thing is, though.

@gbracha
Copy link
Owner

gbracha commented May 17, 2015

Current spec says:

Evaluation of an assignment a of the form e1?.v = e2 is equivalent to the evaluation of the expression ((x) => x == null?null : x.v = e2)(e1). The static type of a is the static type of e2. Let T be the static type of e1 and let y be a fresh variable of type T. Exactly the same static warnings that would be
caused by y.v = e2 are also generated in the case of e1?.v = e2.

In other words, e2 is not evaluated if e1 is null. This is consistent with our treatment of method arguments in general. Basically, if the target is null, nothing happens. Very similar to writing

(a = e1) != null ? a.x = e2 : null

which is presumably what you would have had to write before this worthy construct was introduced.

@nex3
Copy link

nex3 commented May 17, 2015

👍

@sigmundch
Copy link
Author

great, thanks for the clarification!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants