`Ord` between integer types

Why PartialOrd (and also Ord) is not implemented for all pairs of integer types?
It seems to me that it is always possible and valid to compare to integers, whatever their type is.

I usually write code that requires me to use different integer types, mostly unsigned. And often I have to compare two values that has different unsigned integer types. Typically that's because some limitation is expressed in u32 or u64 and should be compared against array length.

Maybe there was a discussion with decision to NOT implement comparison between integer types. If so I'd like to read it.

3 Likes

Ord is not parameterized, so it can only compare with itself, but it would be possible for PartialOrd.

We recently added something similar to num-bigint (not yet published), comparing the big integers with primitive integer types. However, I'm getting ready to remove this (issue, pr), because it had some really nasty effects on type inference from afar.

Right now, with primitive integers only having a single PartialOrd implementation, type inference knows exactly how to resolve the types. But as soon as there's another possibility for the comparison, the compiler needs you to be explicit about which one you meant.

4 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.