Unit Type - in Programming Languages

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, the unit type is called and its only value is also , reflecting the 0-tuple interpretation.
  • In ML (including OCaml and Standard ML), 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. NIL itself is used as the name of the bottom type.
  • In Python, there is a type called NoneType which allows the single value of None.
  • In C++11, there is a type called nullptr_t (null pointer type) that can only store the value nullptr.

The unit type is useful even in functions without side effects if the programming language supports algebraic data types. Any nullary data constructor is effectively isomorphic with the unit type. One can solve the semipredicate problem (the problem of distinguishing between a "normal" return value of a function and an "error") elegantly in such a language, by encoding the "error" as the unit type. In Haskell, the polymorphic type Maybe is predefined for this purpose. Here Nothing is isomorphic to the unit type:

data Maybe a = Nothing | Just a

The type Maybe is called the option type in type theory.

Read more about this topic:  Unit Type

Famous quotes containing the words programming and/or languages:

    If there is a price to pay for the privilege of spending the early years of child rearing in the driver’s seat, it is our reluctance, our inability, to tolerate being demoted to the backseat. Spurred by our success in programming our children during the preschool years, we may find it difficult to forgo in later states the level of control that once afforded us so much satisfaction.
    Melinda M. Marshall (20th century)

    Science and technology multiply around us. To an increasing extent they dictate the languages in which we speak and think. Either we use those languages, or we remain mute.
    —J.G. (James Graham)