ArticleslgStudy

science

Unit type

Unit type is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Unit type rather than just read about it. In short: 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.

Key takeaways

  • Unit type belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Unit type to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Unit type from memory before moving on to harder problems.

Reference excerpt

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 NULL is a unit type which has one value, namely the symbol NIL. This should not be confused with the NIL 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 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:

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:

For brevity, UNIT is not implemented as a singleton; see singleton pattern for further explanation on singleton units.

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 type Void 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 is possible to declare functions that may only return null (in fact, typeof(null) is 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 Pierce, Benjamin C. (2002). Types and Programming Languages. MIT Press. pp. 118–119. ISBN 0-262-16209-1. unit type at the nLab

Worked examples

Example 1 — a first encounter with Unit type

Start with the simplest possible case. Write down what Unit type claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Unit type before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Unit type ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Unit type

In research
Unit type appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Unit type in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Unit type is common in secondary-school and first-year university syllabi. It links to neighbouring topics Data types, Type theory, so understanding it makes those chapters shorter.
In everyday life
Look for Unit type outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Unit type in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Unit type means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Unit type out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Unit type in simple terms?

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.

Why does Unit type matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Unit type?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Unit type.

Tags

  • Data types
  • Type theory

Keep exploring