Neha Patil (Editor)

Option type

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit

In programming languages (more so functional programming languages) and type theory, an option type or maybe type is a polymorphic type that represents encapsulation of an optional value; e.g., it is used as the return type of functions which may or may not return a meaningful value when they are applied. It consists of a constructor which either is empty (named None or Nothing), or which encapsulates the original data type A (written Just A or Some A). Outside of functional programming, these are termed nullable types.

Contents

Names and definitions

In different programming languages, the option type has various names and definitions.

  • In Agda, it is named Maybe with variants nothing and just a.
  • In C++17 it is defined as the template class std::optional<T>.
  • In C#, it is defined as T?.
  • In Coq, it is defined as Inductive option (A:Type) : Type := | Some : A -> option A | None : option A..
  • In Haskell, it is named Maybe, and defined as data Maybe a = Nothing | Just a.
  • In Idris, it is also defined as data Maybe a = Nothing | Just a.
  • In Java, since version 8, it is defined as parameterized final class Optional<T>.
  • In Julia, the option type is named Nullable{T}.
  • In Kotlin, it is defined as T?.
  • In OCaml, it is defined as type 'a option = None | Some of 'a.
  • In Perl 6, this is the default, but you can add a :D "smiley" to opt into a non option type.
  • In Rust, it is defined as enum Option<T> { None, Some(T) }.
  • In Scala, it is defined as parameterized abstract class '.. Option[A] = if (x == null) None else Some(x)...
  • In Standard ML, it is defined as datatype 'a option = NONE | SOME of 'a.
  • In Swift, it is defined as enum Optional<T> { case none, some(T) } but is generally written as T? and is initialized with either a value or nil.
  • In type theory, it may be written as: A ? = A + 1 .

    In languages having tagged unions, as in most functional programming languages, option types can be expressed as the tagged union of a unit type plus the encapsulated type.

    In the Curry-Howard correspondence, option types are related to the annihilation law for ∨: x∨1=1. the argument An option type can also be seen as a collection containing either one or zero elements.

    The option monad

    The option type is a monad under these functions:

    return : A A ? = a Just a bind : A ? ( A B ? ) B ? = a f { Nothing if   a = Nothing f a if   a = Just a

    We may also describe the option monad in terms of functions return, fmap and join, where the latter two are given by:

    fmap : ( A B ) A ? B ? = f a { Nothing if   a = Nothing Just f a if   a = Just a join : A ? ? A ? = a { Nothing if   a = Nothing Nothing if   a = Just Nothing Just a if   a = Just Just a

    The option monad is an additive monad: it has Nothing as a zero constructor and the following function as a monadic sum:

    mplus : A ? A ? A ? = a 1 a 2 { Nothing if   a 1 = Nothing a 2 = Nothing Just a 2 if   a 1 = Nothing a 2 = Just a 2 Just a 1 if   a 1 = Just a 1

    The resulting structure is an idempotent monoid.

    Scala

    Scala implements Option as a parameterized type, so a variable can be an Option, accessed as follows:

    Two main ways to use an Option value exist. The first, not the best, is the pattern matching, as in the first example. The second, the best practice, is the monad method, as in the second example. In this way, a program is safe, as it can generate no exception or error (e.g., by trying to obtain the value of an Option variable that is equal to None). Thus, it essentially works as a type-safe alternative to the null value.

    Rust

    Rust allows using either pattern matching or optional binding to deconstruct the Option type:

    Julia

    Julia requires explicit deconstruction to access a nullable value:

    Perl 6

    There are as many null values as there are types, that is because every type is its own null. So all types are also their own option type.

    To opt into a non-nullable version of a type add the :D "smiley" to it.

    It is also possible to opt into a type that is only ever a nullable using the :U "smiley".

    Type "smileys" are used more often for methods and subroutines than they are for variables.

    There are special variations of if and unless called with and without that check for definedness rather than truthiness.

    These set $_ by default, unlike their boolean cousins.

    There are also variations of ||, or and and that test for definedness.

    References

    Option type Wikipedia