3 ms·
Yes. Make illegal states unrepresentable.
by lmm 4y ago
Yes. Make illegal states unrepresentable.
- AnimalMuppet 4y agoAnd how exactly do you propose to do that? Here's a variable length array. Valid indexes are either [0...length-1] or [1...length], depending on which indexing arrangement you prefer. You can make -1 impossible by using an unsigned type. But for the index of a variable length array, how do you make length+1 unrepresentable?
- lmm 4y agoUse some kind of range type, or, better, a type that represents "index into this array" (which needs some form of dependent typing, but is very much doable).
- AnimalMuppet 4y agoA range type has the problem that it's not necessarily valid indexes for this array - it could be for some other array. "Index into this array" is, as you say, better (presumably it keeps a pointer or a reference or something to the array), but even that isn't bulletproof. Something like realloc could shrink the array but keep the same pointer, or the array could be deallocated and a new one allocated that happened to get the same address. What you really need is that the array keeps track of its own limits - something like std::vector or Java's ArrayList. But then you're back to the overhead of checking each access. So... I'm still not seeing how you're making illegal states unrepresentable.
- lmm 4y ago> "Index into this array" is, as you say, better (presumably it keeps a pointer or a reference or something to the array), but even that isn't bulletproof. Something like realloc could shrink the array but keep the same pointer, or the array could be deallocated and a new one allocated that happened to get the same address. You can address that with some concept of lifetimes or ownership. > What you really need is that the array keeps track of its own limits - something like std::vector or Java's ArrayList. But then you're back to the overhead of checking each access. IME that's less good than having a dedicated type for "index into this array", even if the latter isn't perfect. A bounds-checked access using an offset from a different array might not be reading from uninitialized memory, but it's still overwhelmingly likely to be a programming bug.
- LorenPechtel 4y agoThe problem is that you don't always have a legal state, especially at object creation. If you forbid illegal states you end up with fake objects taking the place of non-existent real ones. (What's Find supposed to return if it's not there??) In most cases it's better to blow up when you try to work with a null than to mistakenly work with something that takes the place of a null. A null is easier to detect and thus the better solution. Yeah, well implemented nullable and not-nullable types are good but the compilers didn't have that kind of smarts in the old days.
- lmm 4y ago> If you forbid illegal states you end up with fake objects taking the place of non-existent real ones. You don't use a fake object, you use a real one of a different type. In the cases where what you want is "maybe this thing, maybe nothing" you can use an option/maybe type. In the cases where you want something else (e.g. "either this thing or an error message") you can use a type that represents that. > Yeah, well implemented nullable and not-nullable types are good but the compilers didn't have that kind of smarts in the old days. ML had well typed optionals back in the '70s. There's no excuse for using a language that needs null today.
- GoblinSlayer 4y agoIn C++ std::optional<T>::value throws an std::bad_optional_access exception when the value is missing. How is this different from a NullPointerException?
- lmm 4y ago> How is this different from a NullPointerException? It's different because most things can't throw it. The problem isn't that your language has a way to implement a value that might be absent, the problem is that your language does't have a way to implement a (heap) value that won't be absent, except by convention.
- GoblinSlayer 4y ago