2 ms·
It's not about operator precedence, it's about sequence points. Assignment—including in ++—is a side-effecting operation. An expression form that does side effe
by munificent 11y ago
It's not about operator precedence, it's about sequence points. Assignment—including in ++—is a side-effecting operation. An expression form that does side effects can lead to very confusing code like:
foo(a++, a++);
What values get passed foo()? In some languages, it's undefined (!). In others, there is a well-defined answer, but even there the answer may not be intuitive. Removing assignment expression forms avoids this. Note that even "=" is a statement in Go.
It does sacrifice a little expressiveness. But in languages that have dedicated syntax like ranges for iterating over sequential numbers, the ++ and -- operators end up pretty rarely used so it's no big loss.
- masklinn 11y ago> Note that even "=" is a statement in Go. In Python and Rust as well.
- dbaupp 11y agoNitpicking: assignment is an expression that returns () in Rust, meaning `foo(a = 1, a)` is valid if foo has a signature like `fn foo(x: (), y: i32)`. However, arguments are (mostly) defined to be evaluated from left to right.
- kinofcain 11y agoYes, sorry, I should have said sequence points also.
- andy_ppp 11y agoIn my opinion the example given is awful code, yes we should protect developers from themselves but good taste in code is important and incrementing a counter easily should still be a thing. Just stop the bad cases then rather than the well understood and expected usage.
- bjz_ 11y ago> It does sacrifice a little expressiveness. But in languages that have dedicated syntax like ranges for iterating over sequential numbers, the ++ and -- operators end up pretty rarely used so it's no big loss. This is one of the big points too, that often gets overlooked. The removal of ++ fits nicely with the removal of C-style for loops.