Why does assignment return the old value instead of the new one?

164 Views Asked by At

Why does assignment return the previous value instead of the new value, re example: assigns 0 to y instead of 2? This is dangerously unusual as it violates the principle of least surprise.

var x: I32 = 0
let y = (x = 2)
1

There are 1 best solutions below

0
On

I am pretty sure this was done to get consistent results for iso variables. Assignment to an iso variable would not be able to return the new value because that created an alias. But it's true that a less surprising design would involve an assignment operator returning None and some other operation (swap?) for the recovery of the original value that is being overwritten.