Unit type is a type with exactly one possible value.

Unit is used when an expression or function must have a type, but there is no meaningful value to return.

Examples include:

  • () in Rust
  • () in Haskell
  • Unit in Scala

Unit is related to void, but they are not always identical. void often means a function returns no value, while unit is a real type with a single value.

Unit types are useful in languages where every expression has a type.

Anki

id: unit-type-definition deck: Computer Science::Data Types tags: data-types unit-type

Q: What is a unit type? A: A unit type is a type with exactly one possible value.

id: unit-type-vs-void deck: Computer Science::Data Types tags: data-types unit-type void

Q: How is unit related to void? A: Void often means a function returns no value, while unit is a real type with a single value.