3 ms·
> how do you represent "this exists or it doesn't exist" in the type system if you can't do a union type of "X" and "null"? Like this, pick your flavor: https
by kroltan 8y ago
> how do you represent "this exists or it doesn't exist" in the type system if you can't do a union type of "X" and "null"?
Like this, pick your flavor:
https://doc.rust-lang.org/std/option/enum.Option.html https://doc.rust-lang.org/std/option/enum.Option.html
https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html https://docs.oracle.com/javase/8/docs/api/java/util/Optional...
https://docs.microsoft.com/en-us/dotnet/api/system.nullable-1 https://docs.microsoft.com/en-us/dotnet/api/system.nullable-...
https://wiki.haskell.org/Maybe https://wiki.haskell.org/Maybe
If you don't want to follow links: Instead of having nulls, have a type which is either a discriminated union (not possible in JS) or is a (isPopulated, value) pair, and accessing the value is an exception if isPopulated=false.
Granted, that would be an extremely breaking change for a JS-compatible language, which would make it in fact incompatible. But answering the question, that's it.