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 ?+= 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 |
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 ?= b
is basicallya? || a = b
, which is all well and good; it makes sense to want to assign toa
if it's nullish.However, prior to this commit, the code
a ?+= b
was basicallya? || a += b
, which is useless. Ifa
is nullish, I certainly don't want to add anything to it.Contrast with accessignment, where
a?=b
is basicallya? && a.=b
. Obviously better this way.This commit makes
a ?_= b
meana? && a _= b
, as with accessignment, for any compound assignment operator. (a ?= b
anda ?:= 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.