tc39 / proposal-logical-assignment

A proposal to combine Logical Operators and Assignment Expressions
https://tc39.es/proposal-logical-assignment/
MIT License
300 stars 13 forks source link

Confusing behavior relative to existing assigment operators #14

Closed bchociej closed 4 years ago

bchociej commented 4 years ago

Because the existing assignment operators behave like so:

x += y;
x = x + y;

x -= y;
x = x - y;

// and so on

I would expect the proposed operators to behave in a similar way:

a ||= b;
a = a || b;

a &&= b;
a = a && b;

a ??= b;
a = a ?? b;

I am making no comment on the usefulness of either approach, just that I find the meaning of the operators as proposed to be confusing in light of the behavior of the existing assignment operators.

rkirsling commented 4 years ago

See #3.

jridgewell commented 4 years ago

3 deals with this, and we discussed in the latest meeting (see slides 7+).

In particular, this desguaring is over-simplified. (And it whether we run a ?? (a = b) or a = a ?? b is unobservable anyway!) But when it's more complicated, it becomes obvious that += isn't just "copy the left side to the right side". The internal semantics of the operator are different.