3 ms·
I assume it's a habit formed from guarding against unintentional assignment. Less so for most comparisons, but for equality, it'll throw an error if you're usi
by bena 5y ago
I assume it's a habit formed from guarding against unintentional assignment.
Less so for most comparisons, but for equality, it'll throw an error if you're using the wrong operator instead of having unintended side effects.
week = 2180 will set the week to 2180 in a lot of languages.
2180 = week will always throw an error.
So if I want to compare, it's safer to use the form of 2180 == week because if I forget the second '=', the compiler will tell me before I make bigger problems.
- tialaramex 5y agoThis mistake also requires that the assignment operator has a result (and further, that the result can be silently coerced into a boolean for whatever reason) In both Rust and Swift, this mistake doesn't compile, their assignment operators don't have a result and so it can't very well be true (or false) ‡ ‡ Technically the result of Rust's assignment operators is the empty tuple, which is the closest to "doesn't have a result" in the type system. I don't know about Swift.
- floatingatoll 5y agoThat is a stellar point, thank you for this.