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.
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!