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 None or Nothing), or which encapsulates the original data type A (often written Just A or Some A).

A distinct, but related concept outside of functional programming, which is popular in object-oriented programming, is called nullable types (often expressed as A?). The core difference between option types and nullable types is that option types support nesting (e.g. Maybe (Maybe String)Maybe String), while nullable types do not (e.g. String?? = String?).

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 (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.[1] An option type is a particular case of a tagged union, where the Nothing 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.[2]

The option type is also a monad where:[3]

return = Just -- Wraps the value into a maybe

Nothing  >>= f = Nothing -- Fails if the previous monad fails
(Just x) >>= f = f x     -- Succeeds when both monads succeed

The monadic nature of the option type is useful for efficiently tracking failure and errors.[4]

Examples


Ada

Ada does not implement option-types directly, however it provides discriminated types which can be used to parameterize a record. To implement an 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 (hence the null keyword).
  type Optional (Has_Element : Boolean) is record
    case Has_Element is
      when False => Null;
      when True  => Element : Element_Type;
    end case;
  end record;
end Optional_Type;

Example usage:

   package Optional_Integers is new Optional_Type
      (Element_Type => Integer);
   Foo : Optional_Integers.Optional :=
      (Has_Element => True, Element => 5);
   Bar : Optional_Integers.Optional := 
      (Has_Element => False);

Agda

In Agda, the option type is named Maybe with variants nothing and just a.

ATS

In ATS, the option type is defined as

datatype option_t0ype_bool_type (a: t@ype+, bool) = 
	| Some(a, true) of a
 	| None(a, false)
stadef option = option_t0ype_bool_type
typedef Option(a: t@ype) = [b:bool] option(a, b)
#include "share/atspre_staload.hats"

fn show_value (opt: Option int): string =
	case+ opt of
	| None() => "No value"
	| Some(s) => tostring_int s

implement main0 (): void = let
	val full = Some 42
	and empty = None
in
	println!("show_value full → ", show_value full);
	println!("show_value empty → ", show_value empty);
end
show_value full → 42
show_value empty → No value

C++

Since C++17, the option type is defined in the standard library as template <typename T> optional<T>. divide(double x, double y) noexcept {\n\tif (y != 0.0) {\n\t\treturn x / y;\n }\n\n\treturn nullopt;\n}\n\nvoid readDivisionResults(int x, int y) {\n optional<double> result = divide(x, y);\n if (result) {\n std::println(\"The quotient of x: {} and y: {} is {}.\", x, y, result.value());\n } else {\n std::println(\"The quotient of x: {} and y: {} is undefined!\", x, y);\n }\n}\n\nint main(int argc, char* argv[]) {\n readDivisionResults(1, 5);\n readDivisionResults(8, 0);\n}\n"}}'>

import std;

using std::nullopt;
using std::optional;

constexpr optional<double> divide(double x, double y) noexcept {
	if (y != 0.0) {
		return x / y;
    }

	return nullopt;
}

void readDivisionResults(int x, int y) {
    optional<double> result = divide(x, y);
    if (result) {
        std::println("The quotient of x: {} and y: {} is {}.", x, y, result.value());
    } else {
        std::println("The quotient of x: {} and y: {} is undefined!", x, y);
    }
}

int main(int argc, char* argv[]) {
    readDivisionResults(1, 5);
    readDivisionResults(8, 0);
}


In C++23, support for monadic operations for std::optional<T> is available.

In Elm, the option type is defined as type Maybe a = Just a | Nothing.[6]