Remix.run Logo
clnhlzmn 4 hours ago

The way we do it in modern languages with things like std::optional and even that is not the best example.

MBCook 4 hours ago | parent [-]

And higher level languages that works. But what do you do when you get down to low level C or assembly?

You basically end up with null/0 don’t you?

paavohtl 9 minutes ago | parent | next [-]

Rust is a significantly higher level language than C, but it can be used it almost all environments where C is used; provided there's a supported compiler target for it. In (safe) Rust, null is basically a guaranteed compiler optimization. Optional / nullable values are represented via Option<T>, which is a sum type of Some(T) and None. When a reference or other pointer-like value (e.g. Box<T>, an owned heap allocation) is wrapped in Option, the compiler can use the invalid bit patterns of T (such as null) to represent the None variant. This is called niche optimization.

So yes, it's nulls underneath, but the developer never has to think about them.

dietr1ch 24 minutes ago | parent | prev [-]

Eventually you end up with registers that probably allow for 2^N values. But the point is not thinking about the machine executing the instructions, but the construction on top of it that has a safer design.

Seeking performance we've been very prone to avoid abstractions and over and over again have shown why we need the safe abstractions.