Remix.run Logo
atherton94027 5 hours ago

Genuinely curious, how would you handle cases where a value is unset without NULL? This is a legitimate case that happens a lot in eg data modeling

clnhlzmn 4 hours ago | parent | next [-]

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 15 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 30 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.

pdimitar 4 hours ago | parent | prev | next [-]

Sum types, of course.

atherton94027 25 minutes ago | parent [-]

How are you going to build sum types in a way where you can interact with assembly or machine code? The CPU doesn't know about that stuff

jibal 4 hours ago | parent | prev [-]

They already said:

> use the type system to help us use special values safely

... but this is not the place to explain what a type system is or what sum types/maybe/optional/etc. are.