Integer handling is broken
Floating point can be tricky. You can’t really check for equality, and
with IEEE 754 you have a bunch of fun things like values of not a
number
, infinities, and positive and negative zero.
But integers are simple, right? Nope.
I’ll use “integers” to refer to all integer types. E.g. C’s int,
unsigned int, gid_t, size_t, ssize_t, unsigned long long
, and Java’s
int, Integer
, etc…
Let’s list some problems:
- You can’t parse unsigned integers in C/C++ with standard functions.
- Casting between types silently discards information (C, Java, Go, Rust, Haskell).
- Casting between some other types changes the semantic meaning of the number, even when technically no information is lost.
- The state of integers in Javascript is its own mess.
What’s wrong with casting?
Casting an integer from one type to another changes three things:
- The type in the language’s type system.
- Crops values that don’t fit.
- May change the semantic value, by changing sign.
The first is obvious, and is even safe for the language to do implicitly. Why even bother telling the human that a conversion was done?
But think about the other two for a minute. Is there any reason that you want your Continue reading