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

invert sense of ? with compound assignment #969

Merged
merged 1 commit into from
Jun 26, 2017

Conversation

rhendric
Copy link
Collaborator

The code a ?= b is basically a? || a = b, which is all well and good; it makes sense to want to assign to a if it's nullish.

However, prior to this commit, the code a ?+= b was basically a? || a += b, which is useless. If a is nullish, I certainly don't want to add anything to it.

Contrast with accessignment, where a?=b is basically a? && a.=b. Obviously better this way.

This commit makes a ?_= b mean a? && a _= b, as with accessignment, for any compound assignment operator. (a ?= b and a ?:= b are unchanged.)

Technically a breaking change, but I don't see how the feature would have been put to any good use before, so I'm treating this as a minor bug fix and waiting one week before merging on June 26, unless, as always, there are objections.

The code `a ?= b` is basically `a? || a = b`, which is all well and
good; it makes sense to want to assign to `a` if it's nullish.

However, prior to this commit, the code `a ?+= b` was basically `a? || a
+= b`, which is useless. If `a` is nullish, I certainly don't want to
add anything to it.

Contrast with accessignment, where `a?=b` is basically `a? && a.=b`.
Obviously better this way.

This commit makes `a ?_= b` mean `a? && a _= b`, as with accessignment,
for any compound assignment operator. (`a ?= b` and `a ?:= b` are
unchanged.)
@vendethiel
Copy link
Contributor

vendethiel commented Jun 20, 2017

Which case does this serve that &&_= doesn't? Maybe we should just forbid it.

@rhendric
Copy link
Collaborator Author

? and && differ on falsy-but-not-nullish values (0 and the empty string). So

actor.health ?+= 10

increments actor’s health if it exists (even if it's 0), whereas

actor.health &&+= 10

only does so if actor has a nonzero health.

I feel like there are at least as many, if not more, use cases for the former as for the latter.

Again, I'm shooting for consistency with what accessignment does: compare actor.health?=add 10, if health for some reason is an object with an add method instead of a primitive number. Forbidding this with operators but permitting it with accessignment doesn't make a lot of sense to me, though I'm open to arguments that, e.g., the operator version is more confusing for some reason.

@vendethiel
Copy link
Contributor

I'm not too happy on a ?= b being different from a?=b either, tbh. I know why it's different, but I think requiring an explicit a?.=b isn't too bad.

@rhendric
Copy link
Collaborator Author

That seems like a completely independent question—or am I missing your point? Even with the explicit dot, a ?.= b does nothing if a is nullish, and I'm making the case that a ?+= b should similarly do nothing if a is nullish—basically, compound assignment operators are more like accessignment then they are like simple assignment, in that they both read and write the LHS. Any further concerns about that?

@vendethiel
Copy link
Contributor

No, that's a completely independent point indeed.
I'm fine with this PR as-is, I only have doubts that it won't actually be used.

@rhendric rhendric merged commit 8024b39 into gkz:master Jun 26, 2017
This was referenced Jan 11, 2018
@rhendric rhendric deleted the invert-qmark-compound-assign branch May 4, 2023 20:16
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

Successfully merging this pull request may close these issues.

None yet

2 participants