- Nullable type
-
In programming, nullable types are a feature of some statically-typed programming languages which allows a data type to be set to the special value NULL instead of their common range of possible values. For value types or built-in data types like integers and booleans however, such behavior is mostly not possible. Nullable type support allows for the programmer to make also these value types NULL. This can be useful in general and also when working with databases. A field in a Relational database like SQL may have an entry that is NULL (or empty) instead of containing a value. A language with nullable type support can then return a NULL value and correctly represent the behavior of the database.
Contents
Example
An integer variable may contain a positive or negative natural number ( ..., -2, -1, 0, 1, 2, ...). The value 0 (zero) however is not the correct representation for this variable to contain nothing (it contains the value 0 and only the conception in the given context might suggest that 0 represents "nothing" instead of the actual representation: "nothing" of "something"). In many circumstances it is required to represent also the fact that a variable has not been given any value at all. This 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 even 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
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. In some languages, like Objective-C[5], however, NULL object pointers can be used without problems.
Compared with option types
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[6]
Language support
The following programming languages support nullable types:
- Java
- Oxygene
- .NET Framework version 1.0 languages with the external NullableTypes library[2]
This implementation adheres to the Null Object pattern, adheres to the NULL (SQL) Three-valued logic (3VL), includes all the .NET built-in types, requires programmers to implement the nullable behavior for a custom value-type. - .NET Framework version 2.0 languages
This implementation violates the Null Object pattern, violates the NULL (SQL) Three-valued logic (3VL), makes use of the .NET 2.0 Generics so it includes all the .NET built-in types and can easily extends a custom value-type - Perl scalar variables default to
undef
and can be set toundef
. Similarly, list and hash variables can be()
or{}
respectively.
See also
References
- ^ Nullable Types (C# Programming Guide)
- ^ a b Nullable Types for .NET 1.0
- ^ Nullable Types for .NET 1.0 Programming Guide
- ^ Tony Hoare (2009). "Null References: The Billion Dollar Mistake". QCon London. http://qconlondon.com/london-2009/presentation/Null+References:+The+Billion+Dollar+Mistake.
- ^ Objective-C documentation on Sending Messages to nil
- ^ Martin Fowlers' description of the Special Case pattern
Categories:
Wikimedia Foundation. 2010.