In programming languages
Several computer()
and its only value is also ()
, reflecting the 0-tuple interpretation.
*In ML descendants (including OCaml, Standard ML, and F#), the type is called unit
but the value is written as ()
.
*In Scala, the unit type is called Unit
and its only value is written as ()
.
*In Common Lisp the type named is a unit type which has one value, namely the symbol . This should not be confused with the ''type'', which is the bottom type.
* In Python, there is a type called NoneType
which allows the single value of None
. In Python's optional static type annotations, this type is represented as None
.
* In Swift, the unit type is called Void
or ()
and its only value is also ()
, reflecting the 0-tuple interpretation.
* In Void
and its only value is null
.
* In Go, the unit type is written struct
and its value is struct
.
* In PHP, the unit type is called null, which only value is NULL itself.
* In Null
(its only value is null
) and Undefined
(its only value is undefined
) are built-in unit types.
* in Kotlin, Unit
is a singleton with only one value: the Unit
object.
* In Ruby, nil
is the only instance of the NilClass
class.
* In C++, the std::monostate
unit type was added in C++17. Before that, it is possible to define a custom unit type using an empty struct such as struct empty
.
* In Dart, both Null
and the empty record (()
, since Dart 3) have a single possible value, respectively, null
and ()
.
Void type as unit type
In C, C++, C#, D, and PHP, void
is used to designate a function that does not return anything useful, or a function that accepts no arguments. The unit type in C is conceptually similar to an empty struct
, but a struct without members is not allowed in the C language specification (this is allowed in C++). Instead, 'void
' is used in a manner that simulates some, but not all, of the properties of the unit type, as detailed below. Like most imperative languages, C allows functions that do not return a value; these are specified as having the void return type. Such functions are called procedures in other imperative languages like Pascal, where a syntactic distinction, instead of type-system distinction, is made between functions and procedures.
Difference in calling convention
The first notable difference between a true unit type and the void type is that the unit type may always be the type of the argument to a function, but the void type cannot be the type of an argument in C, despite the fact that it may appear as the sole argument in the list. This problem is best illustrated by the following program, which is a compile-time error in C:void
type carries no information, it is useless to pass it anyway; but it may arise in generic programming, such as C++ templates, where void
must be treated differently from other types. In C++ however, empty classes are allowed, so it is possible to implement a real unit type; the above example becomes compilable as:
the_unit
is really a singleton; see singleton pattern for details on that issue.)
Difference in storage
The second notable difference is that the void type is special and can never be stored in a record type, i.e. in a struct or a class in C/C++. In contrast, the unit type can be stored in records in functional programming languages, i.e. it can appear as the type of a field; the above implementation of the unit type in C++ can also be stored. While this may seem a useless feature, it does allow one for instance to elegantly implement a set as a map to the unit type; in the absence of a unit type, one can still implement a set this way by storing some dummy value of another type for each key.In Generics
In Java Generics, type parameters must be reference types. The wrapper typeVoid
is often used when a unit type parameter is needed. Although the Void
type can never have any instances, it does have one value, null
(like all other reference types), so it acts as a unit type. In practice, any other non-instantiable type, e.g. Math
, can also be used for this purpose, since they also have exactly one value, null
.
Null type
Statically typed languages give a type to every possible expression. They need to associate a type to the null expression. A type will be defined for null and it will only have this value. For example in D, it's possible to declare functions that may only return null:See also
* Singleton pattern (where a particular class has only one instance, but narrowly-typed non-nullable references to it are usually not held by other classes)Notes
References
* * {{Data types Data types Type theory Articles with example Java code