5 ms·
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
by atherton94027 3mo 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 3mo agoThe way we do it in modern languages with things like std::optional and even that is not the best example.
- MBCook 3mo agoAnd 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?
- dietr1ch 3mo agoEventually 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.
- paavohtl 3mo agoRust 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.
- pdimitar 3mo agoSum types, of course.
- atherton94027 3mo agoHow 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
- pdimitar 3mo agoOCaml / Rust / Haskell. Apparently they found a way to have the CPU know... about "this stuff".
- imtringued 3mo agoSum types map down to reading a tag and doing a comparison against fixed values. I don't know what to tell you, but you're clearly not cut out to be a software developer in either machine code, assembly or C or any other language if you don't understand something this basic.
- bigstrat2003 3mo agoThat's unnecessarily rude, and untrue in any case. Everyone has to learn stuff sometime, and most people won't naturally run into the implementation details of how higher level languages get translated into machine code.
- pdimitar 3mo agoI agree his tone was not productive but the comment he responded to seemed like a disingenuous argument as well. "The CPU doesn't know about that stuff" is not true -- or it's arguing in bad faith. I mean, hello, tagged unions, all of us with some experience can write a C program that works with those. It's 100% false to say what he said.
- atherton94027 3mo ago
- jibal 3mo agoThey 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.