Nullable type

From Wikipedia, the free encyclopedia
Jump to: navigation, search

In programming, nullable types are a feature of the type system of some programming languages which allow the value to be set to the special value NULL instead of the usual possible values of the data type. In statically-typed languages, a nullable type is an option type (in functional programming terms), while in dynamically-typed languages (where values have types, but variables do not), equivalent behavior is provided by having a single null value.

Primitive types such as integers and booleans cannot generally be null, but the corresponding nullable types (nullable integer and nullable boolean, respectively) can also assume the NULL value. NULL is frequently used to represent a missing value or invalid value, such as from a function that failed to return or a missing field in a database, as in NULL in SQL.

Example[edit]

An integer variable may represent integers, but 0 (zero) is a special case because 0 in many programming languages can mean 'false'. Also this doesn't give us any notion of saying that the variable is empty, a need for which occurs in many circumstances. This need can be achieved with a Nullable Type. In programming languages like C# 2.0 a Nullable integer for example can be declared by a question mark (int? x).[1] In programming languages like C# 1.0 Nullable Types can be defined by an external library[2] as new types (e.g. NullableInteger, NullableBoolean).[3]

A boolean variable makes the effect more clear. Its values can be either "true" or "false", while a nullable boolean may also contain a representation for "undecided". However, the interpretation or treatment of a logical operation involving such a variable depends on the language.

Compared with null pointers[edit]

In contrast, object pointers can be set to NULL by default in most common languages, meaning that the pointer or reference points to nowhere, that no object is assigned (the variable does not point to any object). Nullable references were invented by C.A.R. Hoare in 1965 as part of the Algol W language. Hoare later described their invention as a "billion dollar mistake".[4] This is because object pointers that can be NULL require the user to check the pointer before using it and require specific code to handle the case when the object pointer is NULL.

Compared with option types[edit]

Nullable type implementations usually adhere to the null object pattern.

There is a more general and formal concept that extend the nullable type concept, it comes from option types, which enforce explicit handling of the exceptional case. Option type implementations usually adhere to the Special Case pattern.[5]

Language support[edit]

The following programming languages support nullable types.

Statically typed languages with null include:

Dynamically-typed languages with null include:

  • Perl scalar variables default to undef and can be set to undef.
  • PHP with NULL type and is_null() method
  • Python has the None value.
  • Ruby with nil value and NilClass type.

See also[edit]

References[edit]

  1. ^ "Nullable Types (C#)". Msdn.microsoft.com. Retrieved 2013-08-19. 
  2. ^ "(luKa) Developer Zone - NullableTypes". Nullabletypes.sourceforge.net. Retrieved 2013-08-19. 
  3. ^ "NullableTypes". Nullabletypes.sourceforge.net. Retrieved 2013-08-19. 
  4. ^ Tony Hoare (2009). "Null References: The Billion Dollar Mistake". QCon London. 
  5. ^ "P of EAA: Special Case". Martinfowler.com. Retrieved 2013-08-19.