Is there a reason for not allowing comparing different numerical types without as
casts?
fn main() {
let a: i32 = -1;
let b: u64 = 2;
// none of these compile
a < b;
a > b;
a == b;
a != b;
}
I understand why arithmetics (add, sub, etc) are not allowed and require explicit casting to a common type: it's not always clear what the output type should be. But I don't see any reason why comparisons couldn't be built-in: the output type is always the same (it's bool
) and the operations have clear semantics:
-
iX
andiY
: cast to whichever size is bigger and compare (same foruX
anduY
) -
iX
anduY
: ifiX
is negative than false (or true, depending on the operation), otherwise castiX
touX
and goto 1 -
isize
can be treated just as theiX
, whicheverX
size fits (same forusize
)
To explain my motivation, I have a project that has hundreds of these casts specifically to compare the data points, and:
- it actively hurts readability (especially if the operands are more complex than a variable name, and this forces me to enclose them in parenthesis sometimes)
- it interrupts my mind flow when I write the code - I don't care which common type it'll be, why the compiler couldn't figure it for me?
So, would the compiler team be willing to accept a PR that implements this functionality? Does a change like this require an RFC?