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. An option type is a particular case of a tagged union, where theNothing is taken as 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:
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.
Examples
Ada
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;
Example usage:
package Optional_Integers is new Optional_Type
;
Foo : Optional_Integers.Optional :=
;
Bar : Optional_Integers.Optional :=
;
Agda
In Agda, the option type is named with variants and.ATS
In ATS, the option type is defined asdatatype option_t0ype_bool_type =
| Some of a
| None
stadef option = option_t0ype_bool_type
typedef Option = option
- include "share/atspre_staload.hats"
case+ opt of
| None => "No value"
| Some => tostring_int s
implement main0 : void = let
val full = Some 42
and empty = None
in
println!;
println!;
end
show_value full → 42
show_value empty → No value
C++
Since C++17, the option type is defined in the standard library as.import std;
using std::nullopt;
using std::optional;
constexpr optional
void readDivisionResults
int main
Elm
In Elm, the option type is defined as.F#
In F#, the option type is defined as.let showValue =
Option.fold "No value"
let full = Some 42
let empty = None
showValue full |> printfn "showValue full -> %s"
showValue empty |> printfn "showValue empty -> %s"
showValue full -> The value is: 42
showValue empty -> No value
Haskell
In Haskell, the option type is defined as.showValue :: Maybe Int -> String
showValue = foldl "No value"
main :: IO
main = do
let full = Just 42
let empty = Nothing
putStrLn $ "showValue full -> " ++ showValue full
putStrLn $ "showValue empty -> " ++ showValue empty
showValue full -> The value is: 42
showValue empty -> No value
Idris
In Idris, the option type is defined as.showValue : Maybe Int -> String
showValue = foldl "No value"
main : IO
main = do
let full = Just 42
let empty = Nothing
putStrLn $ "showValue full -> " ++ showValue full
putStrLn $ "showValue empty -> " ++ showValue empty
showValue full -> The value is: 42
showValue empty -> No value
Java
In Java, the option type is defined the standard library by the class.import java.util.Optional;
public class OptionExample
showValue full -> The value is: 42
showValue empty -> No value
Nim
import std/options
proc showValue: string =
opt.map.get
let
full = some
empty = none
echo "showValue -> ", showValue
echo "showValue -> ", showValue
showValue -> The Value is: 42
showValue -> No value
OCaml
In OCaml, the option type is defined as.let show_value =
Option.fold ~none:"No value" ~some:
let =
let full = Some 42 in
let empty = None in
print_endline ;
print_endline
show_value full -> The value is: 42
show_value empty -> No value
Rocq
In Rocq, the option type is defined as.Rust
In Rust, the option type is defined as.fn show_value -> String
fn main
show_value -> The value is: 42
show_value -> No value
Scala
In Scala, the option type is defined as, a type extended by and.object Main:
def showValue: String =
opt.fold
def main: Unit =
val full = Some
val empty = None
println
println
showValue -> The value is: 42
showValue -> No value
Standard ML
In Standard ML, the option type is defined as.Swift
In Swift, the option type is defined as but is generally written as.func showValue -> String
let full = 42
let empty: Int? = nil
showValue -> The value is: 42
showValue -> No value
Zig
In Zig, add ? before the type name like?i32 to make it an optional type.Payload n can be captured in an if or while statement, such as, and an else clause is evaluated if it is
null.const std = @import;
const File = std.fs.File;
const DebugAllocator = std.heap.DebugAllocator;
const Allocator = std.mem.Allocator;
fn showValue !u8
pub fn main !void
showValue -> The value is: 42
showValue -> No value