Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there languages without "null"?

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. There are several questions about this on SO and P.SE, such as Best explanation for languages without null and Are null references really a bad thing? .

However, I could not find any language that does without them. All the languages I'm familiar with have null, or something similar (e.g. "undefined" in Perl).

I realize that proably every language needs some way to express "absence of a value". However, instead of having "null" or "undefined", this can also be made explicit by using something like Maybe (Haskell) or Optional (Guava). The principal difference to having "null" or "undefined" is that an object can only have "no value" if it has a specific type (Maybe, Optional...). In contrast, "null"/"undefined" is typically a valid value possible for every type.

Are there any languages that do not have nullor a similar concept in this sense?

like image 570
sleske Avatar asked Jan 23 '15 09:01

sleske


People also ask

Which language has no null?

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").

What languages are null safe?

Other languages that use null-safe types by default include JetBrains' Kotlin, Rust, and Apple's Swift.

What language is null?

In C#, null means "no object." Information about null and its usages in C# include: You cannot use 0 instead of null in your programs even though null is represented by the value 0. You can use null with any reference type including arrays, strings, and custom types.

Why does null exist in Java?

In Java, a null value can be assigned to an object reference of any type to indicate that it points to nothing. The compiler assigns null to any uninitialized static and instance members of reference type. In the absence of a constructor, the getArticles() and getName() methods will return a null reference.


3 Answers

Here's an incomplete list of languages that are null-safe in the sense that they don't have any non-nonnullable types:

  • Dart (2021): Has optional types with ? syntax.
  • C# 8 (2019): Has opt-in "nullable reference types".
  • Kotlin (2015): Has optional types with ? syntax.
  • Pony (2015). Uses union type where one of the types is None.
  • Swift (2014): Has optional types with ? syntax.
  • Crystal (2014): Does have nil, but prevents all null pointer exceptions at compile-time.
  • Hack (2014): Has optional types with ? syntax.
  • TypeScript (2012): Has union types that can have undefined or null as a variant.
  • Elm (2012): Has union type Maybe.
  • Ceylon (2011): Has optional types with ? syntax.
  • Rust (2010): Has optional type Option.
  • Fantom (2005): Has optional types with ? syntax.
  • F# (2005): Has union type Option.
  • Nice (2003): Has optional types with ? syntax.
  • Netlogo (1999) has no type null
  • OCaml (1996): Has union type option.
  • Haskell (1990): Has union type Maybe.
  • Standard ML (1990): Has union type option.
  • Tcl (1988)
  • Erlang (1986)
  • Prolog (1972): A logical variable stands for "anything at all". There is no concept of "null" or "undefined".

Feel free to complement the list. The years represent the first public release.

like image 155
emlai Avatar answered Oct 11 '22 21:10

emlai


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.

like image 31
slebetman Avatar answered Oct 11 '22 20:10

slebetman


V is a newer language with Golang-like syntax that has no nulls.

like image 2
chikega Avatar answered Oct 11 '22 19:10

chikega