invert sense of ? with compound assignment#969
Conversation
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.)
|
Which case does this serve that |
|
actor.health ?+= 10increments actor’s health if it exists (even if it's 0), whereas actor.health &&+= 10only 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 |
|
I'm not too happy on |
|
That seems like a completely independent question—or am I missing your point? Even with the explicit dot, |
|
No, that's a completely independent point indeed. |
The code
a ?= bis basicallya? || a = b, which is all well and good; it makes sense to want to assign toaif it's nullish.However, prior to this commit, the code
a ?+= bwas basicallya? || a += b, which is useless. Ifais nullish, I certainly don't want to add anything to it.Contrast with accessignment, where
a?=bis basicallya? && a.=b. Obviously better this way.This commit makes
a ?_= bmeana? && a _= b, as with accessignment, for any compound assignment operator. (a ?= banda ?:= bare 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.