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.
Maybe
with variants nothing
and just a
.std::optional<T>
.T?
.Inductive option (A:Type) : Type := | Some : A -> option A | None : option A.
.data Maybe a = Nothing | Just a
.data Maybe a = Nothing | Just a
.Optional<T>
.Nullable{T}
.T?
. type 'a option = None | Some of 'a
.:D
"smiley" to opt into a non option type.enum Option<T> { None, Some(T) }
.'.. Option[A] = if (x == null) None else Some(x)..
.datatype 'a option = NONE | SOME of 'a
.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:
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:
We may also describe the option monad in terms of functions return, fmap and join, where the latter two are given by:
The option monad is an additive monad: it has Nothing as a zero constructor and the following function as a monadic sum:
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.