unit type

Uses your system voice; voice and rate in settings.

Q1307872Snapshot

≈ 6 min read

Names in other languages

  • Češtinajednotkový typ
  • Русскийединичный тип
  • DeutschEinheitstyp
  • FrançaisType unité
  • Englishunit type
  • etühiktüüp
  • 日本語ユニット型
  • 中文单值类型

Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.

In 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 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 languages== Several 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. In Haskell, Rust, and Elm, the unit type is called () 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 Java, the unit type is called Void and its only value is null . It cannot be instantiated. It is used in generics for denoting return types of void . 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 JavaScript, both 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 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 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: class UnitType {}; constexpr UnitType UNIT; UnitType f(UnitType) noexcept { return UNIT; } UnitType g(UnitType) noexcept { return UNIT; } int main() { f(g(UNIT)); return 0; } For brevity, UNIT is not implemented as a singleton; see singleton pattern for further explanation on singleton units. import java.util.concurrent.*; ExecutorService executorService = Executors.newFixedThreadPool(2); CompletableFuture future = CompletableFuture.runAsync(() -> { try { // Simulate some work System.out.println("Task started."); Thread.sleep(2000); // Simulate a 2-second task System.out.println("Task completed."); } catch (InterruptedException e) { System.err.println("Task was interrupted."); } }, executorService); 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 . 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)); } typeof(null) returnThatSpecialThing() { return null; } void writeTypeOfNull() { writeln(typeof(null)); // prints: void } null is the only value that typeof(null) , a unit type, can have. ==See also== Any type Bottom type 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) Undefined Void type ==Notes== ==References==

Text derived from https://en.wikipedia.org/w/index.php?oldid=1326270108 under CC BY-SA 4.0; modifications have been made.

Provenance

snapshot unit-type-152017-e8a1393b2aefb1cb · hash e8a1393b2aefb1cb6f256eb47a694f1c9cc4476db451fb8f87f60b857bfb715b · captured 1765154934.0 · version history

Cite this page as: encyclopegia.org, “unit type”, snapshot unit-type-152017-e8a1393b2aefb1cb. https://encyclopegia.org/a/unit-type