4 ms·
NULL exists in every programming language. There must be something to designate a missing value, a non assigned pointer or reference.
by user5994461 8y ago
NULL exists in every programming language. There must be something to designate a missing value, a non assigned pointer or reference.
- ancarda 8y agoMost languages, sure, but not all; Haskell doesn’t have NULL. You can express the absense of a value with a Maybe: Just “value” -> Maybe String Nothing -> Maybe a If your function returns a “Maybe String”, it can either be just a value, or Nothing. However, if a function returns a “String”, you can’t return Nothing, only an actual String. There’s no null in Haskell.
- baq 8y agoyeah... no. most notably rust, which might be surprising as it can be used (and has been) to write a kernel in, only has null pointers in unsafe code. rust is hardly the first one, too.
- masklinn 8y ago> NULL exists in every programming language. No. > There must be something to designate a missing value, a non assigned pointer or reference Option types are something like 45 years old, they're not exactly futuristic technology.
- antonvs 8y agoThere has to be a way to represent a missing value, but the key point in languages that don't have NULL is that you can only represent missing values using a type that supports missing values - e.g. option types such as Haskell's Maybe. This achieves two things: 1. It allows you to define functions that cannot return NULL, and the compiler guarantees this. This is a huge win - you no longer have NULL as a possible return value for many functions. 2. Values belonging to the option type have to be unwrapped to access their contents, so it's not possible to unknowingly dereference a missing value. This is an even bigger win than #1, because it can literally eliminate null pointer errors. A caveat to #2 is that depending on the language, it may be possible to override safety, for example Haskell's fromJust can throw an exception if used on a missing value. But this tends to require conscious effort, and can easily be checked for automatically, since it involves the use of unsafe function calls.
- masklinn 8y ago> A caveat to #2 is that depending on the language, it may be possible to override safety, for example Haskell's fromJust can throw an exception if used on a missing value. But this tends to require conscious effort, and can easily be checked for automatically, since it involves the use of unsafe function calls. A much scarier example there is C++, where std::optional can be deref'd (literally `*opt`, the easiest and shortest thing you can do with it)… and just UBs on an empty optional. Because C++ hates both type-safety and memory-safety.