Re:Avoid NULL
by AnimalMuppet on Monday November 09, 2015 @05:35PM
(#10536341)
> All pointer types are nullable, and no non-pointer types are nullable.
It's also error numbers. Yeah, they're just an int as far as types go, but they also have nullable semantics, with 0 being "no error". So it's not just pointers (quite).
> You're right that an optional type is actually two types. But it's designed such that you can't get at the underlying runtime type without first checking to see what it is. That's a huge difference.
Agreed.
I disagree that error numbers are nullable. It's just an integer, and there's nothing special about the zero integer. It's not like pointers where if you dereference NULL, you've hit a special case where anything could happen. There's a difference between a language-level NULL, as pointers have, and a value with no special language meaning which you give special meaning at a higher level.
If you want an example of a non-pointer nullable type, float and double probably qualify. Both can be NAN, which is effectively NULL for floats. Like NULL, NAN behaves completely differently from any other value and only convention and documentation tell you whether any particular place might produce or accept it.
Re:Avoid NULL
by AnimalMuppet on Monday November 09, 2015 @06:21PM
(#10536555)
Don't forget about signalling vs. non-signalling NANs...
If you have an interface which returns a pointer that's never NULL, you still have to return a nullable pointer. The non-nullability of the value can only be encoded in documentation or naming conventions. Because of this, the language has to make it easy to use pointers without first checking for NULL, otherwise all these interfaces would become a giant pain in the ass to use. And that means that when you're working with an interface which can return NULL, you have nothing that forces you to check for that. You just have to remember. It's similar for function parameters.
Optional types also work for non-pointer types, which C's NULL simply cannot do. That's a major plus, beyond the problem of all pointers being nullable.
You're right that an optional type is actually two types. But it's designed such that you can't get at the underlying runtime type without first checking to see what it is. That's a huge difference.