Option type


In 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, or which encapsulates the original data type A.
A distinct, but related concept outside of functional programming, which is popular in object-oriented programming, is called nullable types. The core difference between option types and nullable types is that option types support nesting, while nullable types do not.

Theoretical aspects

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 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.
In the Curry–Howard correspondence, option types are related to the annihilation law for ∨: x∨1=1.
An option type can also be seen as a collection containing either one or zero elements.
The option type is also a where:
return = Just -- Wraps the value into a maybe
Nothing >>= f = Nothing -- Fails if the previous monad fails
>>= f = f x -- Succeeds when both monads succeed
The monadic nature of the option type is useful for efficiently tracking failure and errors.

Names and definitions

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

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:

Generic
-- Any constrained & non-limited type.
Type Element_Type is private;
Package Optional_Type is
-- When the discriminant, Has_Element, is true there is an element field,
-- when it is false, there are no fields.
Type Optional is record
case Has_Element is
when False => Null;
when True => Element : Element_Type;
end case;
end record;
end Optional_Type;

Scala

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

// Defining variables that are Options of type Int
val res1: Option = Some
val res2: Option = None
// sample 1 : This function uses pattern matching to deconstruct Options
def compute = opt match
// sample 2 : This function uses built-in fold method
def compute = opt.fold
println // The value is: 42
println // No value

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 a monadic approach, as in the second example. In this way, a program is safe, as it can generate no exception or error. Thus, it essentially works as a type-safe alternative to the null value.

OCaml

implements Option as a parameterized variant type. Options are constructed and deconstructed as follows:

let compute opt = match opt with
| None -> "No value"
| Some x -> "The value is: " ^ string_of_int x
print_endline
print_endline

F#


let compute = function
| None -> "No value"
| Some x -> sprintf "The value is: %d" x
printfn "%s"
printfn "%s"

Haskell


-- This function uses pattern matching to deconstruct Maybes
compute :: Maybe Int -> String
compute Nothing = "No value"
compute = "The value is: " ++ show x
main :: IO
main = do
print $ compute -- The value is: 42
print $ compute Nothing -- No value

Swift


func compute -> String
print // The value is: 42
print // No value

Rust


fn main

Nim


import options #Module must be imported
proc compute=
if: echo "The Value is: " & $a.get
else: echo "The Value is None"
var
a : Option = option
b : Option
compute #The Value is: 42
compute #The Value is None