by didibus · 3y ago · 0 comments
NotNull, or so called "null safety" makes it that when you know something is not optional, if a null is passed to it, you know it's a bug.
But when you do have null again, by making something nullable, the million dollar problem is back, in that you don't know why it's null?
Is it null because it's missing, false, errored, eof, leaf node, etc. You don't know.
In my opinion what we would need is union types, and simply never use null for anything, remove it completely.
Instead you could declare a function returns a Value or Missing. Or it would return a Value or False. Or it would return a Value or EndOfFile.
And similarly you could say:
User {
String|NotProvided email;
}
Where email on user can be either a String or NotProvided.You wouldn't want the NotProvided, EndOfFile, and all that to be full on classes though, just like type aliases of some sort, similar to null in a way, but it adds semantics.
if (email == NotProvided) {
...
}
And the language compiler would error if you try to use it where it doesn't work without checking: String email = user.email; // Error
if (user.email != NotProvided)
{
String email = user.email
} // No errorSo, for a quick script or rapid prototyping, just unwrap/panic if you don't care about the result, like in a short code contest, and for any other use, refrain from using these, match all possibilities, implement a strongly typed Error management.
In Kotlin (and others) it's extremely ergonomic, too. You just say foo?.bar and coalesce with ?: so
val baz = foo?.bar ?: "foo or bar is null"
In certain cases the Kotlin compiler will infer non-nullable type after an explicit null check just like in your example such that
fun bestFunction(foo : String?) {
if(foo == null) return
foo.baz <- //now legal to call without .?
}For Ops requirement I think it's too specific for a language level feature. In Kotlin you'd just use a sealed type in the cases were you really need to know why function can't produce a value. Works like a charm.