Hacker News new | ask | show | jobs
by smokel 43 days ago
18446744073709551616 possible values and you can't spare 1 for null? :)

TIL that Rust has NonZeroU64 which you can combine with Optional to get the required behaviour with only 64 bits per entry. [1]

[1] https://doc.rust-lang.org/std/num/type.NonZeroU64.html

2 comments

Especially because signed integers aren't symmetrical. Reserve INT_MIN and you get 8 billion NaN values and multiplying by -1 always gives you a valid location.
Ah, INT_MIN. If I had a nickel every time I saw an implementation of inttostr/strtoint that don't properly handle it, I'd probably have about $5. So I too thought about possibility of it serving as a NaN of signed integers... but I doubt we'll see it any time soon.

Also, apparently, shifting a negative number to the left is UB in C.

Left shifting a large positive number is also UB. C made a lot of things undefined that would make much more sense as implementation defined.
Yep. Basically, if you take a signed int/long/etc. and a set bit shifts left into the sign bit — UB.

> a lot of things... would make much more sense as implementation defined.

Or even just defined. Apparently, the fact that shifting right a signed number is done arithmetically, not logically, is implementation-defined too.

Well, at lest bitwise not/and/or are (almost) fully defined even for signed integers, so that's something.

Well at the time it made sense not to force two's complement. But I would try harder to trim the UB list over time.
Java has always had this juxtaposition in language design where they don't want you to think hard about value types (hence no unsigned types), but the types themselves are precisely defined down to the specific bit pattern representation in bytecode. This is essentially hardcoded into the JVM and its bytecode specification and is too hard to change for compatibility concerns.

Luckily we have Valhalla, which is an admission that Gosling was partially wrong, and programmers who want to have an unsigned nullable non-zero 64-bit integral value type can just make one, and not have to pay outsized memory costs to do so.

If we're done paying homage to Gosling, can we get operator overloading for our fancy value types please? I have no idea if this is on the radar for Valhalla.

In many cases, NonMaxU64 and NonMinI64 (aka SymmetricI64) would be much more useful though. But Rust is aiming to eventually add a more general way to create types with user-defined ranges, such as the "pattern types" feature currently being prototyped.