From the proposal I believe a ||= b is syntactic sugar for: a = a || b, but is meant to not trigger the object's setter function (if it has one).
They use the example: obj.a = obj.a ?? b would trigger the setter for obj.a since it's being reassigned in both instances. To prevent this we could do obj.a ?? (obj.a = b); which would only trigger the reassignment if obj.a == null, but apparently that's weird. Thus to make it succinct they propose: a ??= b.
Which will not trigger the setter unless it needs to.
It's just like an assignment, except that it will only get assigned if a is already false. If it isn't, then no assignment, a will still be what it was.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I don't really understand number 4
From the proposal I believe
a ||= b
is syntactic sugar for:a = a || b
, but is meant to not trigger the object's setter function (if it has one).They use the example:
obj.a = obj.a ?? b
would trigger the setter forobj.a
since it's being reassigned in both instances. To prevent this we could doobj.a ?? (obj.a = b);
which would only trigger the reassignment ifobj.a == null
, but apparently that's weird. Thus to make it succinct they propose:a ??= b
.Which will not trigger the setter unless it needs to.
Thanks!
It's just like an assignment, except that it will only get assigned if a is already false. If it isn't, then no assignment, a will still be what it was.