5 ms·
In this respect, Python makes a lot more sense since that is how you'd normally write such an equality in math, and generally how people chain them: A=B=C means
by int_19h 2y ago
In this respect, Python makes a lot more sense since that is how you'd normally write such an equality in math, and generally how people chain them: A=B=C means A=B and B=C.
Part of the problem here is that we treat true/false as "just another value" in programming, and thus the usual operators are used to compare them. In math notation, if you wanted to compare the result of comparison A=B to a Boolean value C, you'd normally write something like (A=B)≡C, using different operators for equality and equivalence.
Interestingly, programming languages haven't always treated Booleans in this way. If you look at ALGOL 60, it starts by cleanly separating expressions into arithmetic and logical ones, and defines completely different sets of operators for each (the reference language also uses = for numeric comparisons and ≡ for Booleans; and boolean inequality is, of course, just XOR).
- analog31 2y agoIndeed, and likewise, I think Pascal would have thrown a compile-time error due to a type mismatch.
- throwaway313373 2y agoI think that the main issue here is not treating true/false as values but allowing them to be implicitly converted or compared to numbers with the assumption that true equals 1 and false equals 0. I think that Rust got this right. It doesn't allow you to add integer to boolean or multiply boolean by float etc, because it is unclear what does it even mean mathematically. Also, most languages implicitly assume that any value of any type is either "truthy" or "falsy" thus you can do something like `while(1) { do_stuff() }`. Rust doesn't allow this BS, `if` and `while` expect a boolean so you have to provide a boolean.
- flakes 2y agoA lot of languages have no boolean primitive to begin with. Often in older languages, the values for `true` and `false` are aliases for `0` and `1` respectively. Perl and earlier versions of C, Python, and JavaScript are notable. Perl is probably the most awkward due to context-sensitive casting. e.g. the string `"0"` in a boolean context evaluates as an integer, and the boolean interpretation for `0` is false.
- erik_seaberg 2y agoSometimes Perl uses "0E0" as a value that is equal to zero yet not false. Javascript has even more cases where equality isn't transitive.
- int_19h 2y agoJavaScript had Booleans as a primitive type as far back as I can remember. The problem, rather, is that it's altogether too eager to convert everything into everything else.
- int_19h 2y agoThat resolves the problem for other types, but you still have the case of (a == b == c) being parsed and evaluated as ((a == b) == c) if all three are booleans, which is still not an intuitive interpretation. I think the PL either has to ban such construct outright and require parentheses, or treat it as an n-ary operator like Python does (albeit perhaps with a few more constraints on what you can chain, for the sake of readability).
- throwaway313373 2y agoI've just checked [0], Rust bans such construct for booleans as well. [0] https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=251e09e9dd66a9e653eace3a075a6a4a https://play.rust-lang.org/?version=stable&mode=debug&editio...
- quietbritishjim 2y agoThat is impressive.
- int_19h 2y agoGreat error message, too.
- the5avage 2y agoI think it makes sense in python for ==, <=, >= I cannot see it for != "a != b != c" is not equal to "not (a == b == c)" which is a bit strange imo
- irishsultan 2y agoI don't see how you could interpret "a != b != c" as equivalent to "not (a == b == c)" in the first place. In the first expression a doesn't equal b and b doesn't equal c (no restriction on a and c). In the second expression you could have a == b, but b != c (and vice versa), clearly that's not equivalent to the first expression.
- the5avage 2y ago"a == b" is the same as "not (a != b)" Maybe I'm simple minded though.
- int_19h 2y agoIf you treat each "!" as a NOT, then (a != b != c) has two NOTs, but !(a == b == c) has a single one, so this is a bit like expecting that !(a && b) is the same as (!a && !b).
- Doxin 2y ago"a == b == c" translates to "a == b and b == c", similarly "a != b !=c" translates to "a != b and b != c"