Unit typeIn the area of mathematical logic and computer science known as type theory, a unit type is a type that allows only one value (and thus can hold no information). The carrier (underlying set) associated with a unit type can be any singleton set. There is an isomorphism between any two such sets, so it is customary to talk about the unit type and ignore the details of its value. One may also regard the unit type as the type of 0-tuples, i.e. the product of no types. The unit type is the terminal object in the category of types and typed functions. It should not be confused with the zero or empty type, which allows no values and is the initial object in this category. Similarly, the Boolean is the type with two values. The unit type is implemented in most functional programming languages. The void type that is used in some imperative programming languages serves some of its functions, but because its carrier set is empty, it has some limitations (as detailed below). In programming languagesSeveral computer programming languages provide a unit type to specify the result type of a function with the sole purpose of causing a side effect, and the argument type of a function that does not require arguments.
Void type as unit typeIn C, C++, C#, D, and PHP, Difference in calling conventionThe 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 f(void) {}
void g(void) {}
int main(void)
{
f(g()); // compile-time error here
return 0;
}
This issue does not arise in most programming practice in C, because since the class unit_type {};
const unit_type the_unit;
unit_type f(unit_type) { return the_unit; }
unit_type g(unit_type) { return the_unit; }
int main()
{
f(g(the_unit));
return 0;
}
(For brevity, we're not worried in the above example whether Difference in storageThe 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 GenericsIn Java Generics, type parameters must be reference types. The wrapper type public static Void f(Void x) { return null; }
public static Void g(Void x) { return null; }
public static void main(String[] args)
{
f(g(null));
}
Null typeStatically 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: typeof(null) returnThatSpecialThing(){
return null;
}
null is the only value that typeof(null), a unit type, can have. See also
Notes
References
|
Portal di Ensiklopedia Dunia