News for hackers. Stuff that matters.
Re:Overhead of Returning Optional Values in Java and Rust by herbstein on Saturday October 16, 2021 @02:54PM (#28890381)
> This is somewhat tangential, but in my opinion implementing Optionals as a "normal" wrapper type, as is done in Java, doesn't really help that much

The great thing about Rust is that `Option` is a normal, generic type. You could very easily implement your own equivalent type.

Re:Overhead of Returning Optional Values in Java and Rust by mqus on Saturday October 16, 2021 @03:48PM (#28890855)
Not quite. The compiler does have the knowledge that it can use \0 for representing None in Option<Pointer> types. I'm not sure it can infer that for home-grown Option enums.

So it can optimize the memory overhead of Option<> away completely.

Re:Overhead of Returning Optional Values in Java and Rust by umanwizard on Saturday October 16, 2021 @03:59PM (#28890948)
That is called “niche optimization” and the compiler will in fact still do it for a hand-rolled Option type.
Re:Overhead of Returning Optional Values in Java and Rust by tialaramex on Saturday October 16, 2021 @07:11PM (#28892408)
Although, Rust only knows about certain obvious niches. It can reason that it can fit None in the same space as NonZeroU64 or a reference &T but it doesn't see how to do this with a hypothetical EvenU64 as I understand it because that "niche" is in the wrong place.
Re:Overhead of Returning Optional Values in Java and Rust by herbstein on Saturday October 16, 2021 @05:51PM (#28891918)
For anyone not sure, here is the proof.

https://godbolt.org/z/4zhWM9hTP