3 ms·
I want to specify Option<T> with exhaustive pattern matching as the only way to extract the value out of the container is the safest way. Optionals without thi
by deltaonefour 4y ago
I want to specify Option<T> with exhaustive pattern matching as the only way to extract the value out of the container is the safest way.
Optionals without this feature/restriction are actually the same as a null.
if(optional_value.has_value())
if(value != NULL)
Both of the above checks are STILL required or it's an error. Optionals only propagate the null to another method call. There has to be exhaustive pattern matching for this feature to truly shine.
- blacksmithgu 4y agoEven optionals with bad 'is null / get' assessors are better than nulls, because they come with nullsafe operators like map / flatMap and the 'get' call usually always errors or panics on failure.
- deltaonefour 4y agoIt's the same thing right? flatMap or Map must call the has_value method to work without crashing.
- masklinn 4y agoIt's not because (I assume) even the shitty optional gives you a signal that the value is nullable and that you have to check. The important part is moving `null` out of every type in the language, and into its own little box that the compiler can tell you about. It's nice if you can make it a "library" utility without holes, but it's also fine if it's not perfect, or even if you have to make it a builtin magic thing, like C# &co. > flatMap or Map must call the has_value method to work without crashing. The implementation details don't really matter to the caller, what matters is that they're always operating in a safe environment without being bothered.
- deltaonefour 4y ago>The implementation details don't really matter to the caller, what matters is that they're always operating in a safe environment without being bothered. It matters because it's not true safety. flatMap is a higher level operation, I can make >=> or >>= work for values with null but that's not true safety. True safety as you said, is no null. But an optional that still generates a runtime error is basically isomorphic to a null. The point of getting rid of null is not null itself, but the associated runtime error that comes with it.