There are many people who think that the concept of the special value null
(as it is used in lanuages like C, Java, C#, Perl, Javascript, SQL etc.) is a bad idea. T
Here's an incomplete list of languages that are null-safe in the sense you described:
None
.nil
, but prevents all null pointer exceptions at compile-time.?
syntax.?
syntax.?
syntax.undefined
or null
as a variant.Maybe
.?
syntax.Option
.?
syntax.Option
.?
syntax.null
option
.Maybe
.option
.Feel free to complement the list. The year represents the first appearance according to Wikipedia.
Tcl has no concept of null whatsoever. Everything is a value and all values have a string representation (typically summarized as "Everything is a String").
The closest thing to null is the empty string.
To convey the concept of "no value" requires some creativity.
Of course, as mentioned above, some people use the empty string to signify no value. For this to work, empty strings cannot be valid in the data set you're processing. Surprisingly, a lot of real world data falls into this category.
Another way to indicate absence of value is to simply throw an error. In some cases this is exactly what should have been done instead of returning some null or error value (an anti-pattern learned from C and a habit that's hard to get rid of).
Yet another way is to return an empty list (a list is Tcl's equivalent of arrays in other languages). The string representation of an empty list is the empty string. But fortunately the string representation of a list containing an empty string is two double quotes: "\"\""
. This difference allows one to differentiate between a list that contains "nothing" and a list that contains a string that has no characters in it.
Finally some people simply indicate the absence of values by simply not declaring the variable (or undeclaring it, which is a thing in tcl). This may sound odd because variables seem to be a compile-time construct (while values are run-time construct) but in tcl everything is run-time. Thus it's possible for code to use non existence of the variable as a signal. Trying to read an undeclared variable results in an error which you can catch. In addition, Tcl also allows you to use introspection to check the state of the interpreter. So you can use [info exist x]
to check if a variable called x
exists.
You already mention Haskell as an example of a language without "null". There are also the languages in the ML family like Standard ML, OCaml or F#. Many dynamically typed languages also do not feature null pointers, scheme would be a good example.