In programming languages (especially 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 (often named <code>None</code> or <code>Nothing</code>), or which encapsulates the original data type <code>A</code> (often written <code>Just A</code> or <code>Some A</code>).
A distinct, but related concept outside of functional programming, which is popular in object-oriented programming, is called nullable types (often expressed as <code>A?</code>). The core difference between option types and nullable types is that option types support nesting (e.g. <code>Maybe (Maybe String)</code> â <code>Maybe String</code>), while nullable types do not (e.g. <code>String??</code> = <code>String?</code>).
In type theory, it may be written as: . This expresses the fact that for a given set of values in , an option type adds exactly one additional value (the empty value) to the set of valid values for . This is reflected in programming by the fact that in languages having tagged unions, option types can be expressed as the tagged union of the encapsulated type plus a unit type. An option type is a particular case of a tagged union, where the <code>Nothing</code> is taken as (nullary constructor for a) singleton type. Tagged unions can generally be implemented by a combination of union types and record types using occurrence typing.
The option type is also a monad where:
The monadic nature of the option type is useful for efficiently tracking failure and errors.
Ada does not implement option-types directly, however it provides discriminated types which can be used to parameterize a record. To implement a Option type, a Boolean type is used as the discriminant; the following example provides a generic to create an option type from any non-limited constrained type:
Example usage:
In Agda, the option type is named with variants and .
In ATS, the option type is defined as
Since C++17, the option type is defined in the standard library as .
In C++23, support for monadic operations for is avaliable.
In Elm, the option type is defined as .
In F#, the option type is defined as .
In Haskell, the option type is defined as .
In Idris, the option type is defined as .
In Java, the option type is defined the standard library by the class.
In OCaml, the option type is defined as .
In Rocq, the option type is defined as .
In Rust, the option type is defined as .
In Scala, the option type is defined as , a type extended by and .
In Standard ML, the option type is defined as .
In Swift, the option type is defined as but is generally written as .
In Zig, add ? before the type name like <code>?i32</code> to make it an optional type.
Payload <var>n</var> can be captured in an if or while statement, such as , and an else clause is evaluated if it is <code>null</code>.