Maybe Type
   HOME

TheInfoList



OR:

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: A^ = A + 1. This expresses the fact that for a given set of values in A, an option type adds exactly one additional value (the empty value) to the set of valid values for A. 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 In programming language theory and proof theory, the Curry–Howard correspondence (also known as the Curry–Howard isomorphism or equivalence, or the proofs-as-programs and propositions- or formulae-as-types interpretation) is the direct relati ...
, option types are related to the annihilation law for ∨: x∨1=1. An option type can also be seen as a
collection Collection or Collections may refer to: * Cash collection, the function of an accounts receivable department * Collection (church), money donated by the congregation during a church service * Collection agency, agency to collect cash * Collectio ...
containing either one or zero elements. 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 (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.


Examples


Agda

In Agda, the option type is named with variants and .


Coq

In Coq, the option type is defined as .


Elm

In Elm, the option type is defined as .


F#

let showValue = Option.fold (fun _ x -> sprintf "The value is: %d" x) "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 (\_ x -> "The value is: " ++ show x) "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 (\_, x => "The value is " ++ show x) "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


Nim

import std/options proc showValue(opt: Option nt: string = opt.map(proc (x: int): string = "The value is: " & $x).get("No value") let full = some(42) empty = none(int) echo "showValue(full) -> ", showValue(full) echo "showValue(empty) -> ", showValue(empty) showValue(full) -> The Value is: 42 showValue(empty) -> No value


OCaml

In OCaml, the option type is defined as . let show_value = Option.fold ~none:"No value" ~some:(fun x -> "The value is: " ^ string_of_int x) let () = let full = Some 42 in let empty = None in print_endline ("show_value full -> " ^ show_value full); print_endline ("show_value empty -> " ^ show_value empty) show_value full -> The value is: 42 show_value empty -> No value


Rust

In Rust, the option type is defined as . fn show_value(opt: Option) -> String fn main() show_value(full) -> The value is: 42 show_value(empty) -> No value


Scala

In Scala, the option type is defined as , a type extended by and . object Main showValue(full) -> The value is: 42 showValue(empty) -> 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(_ opt: Int?) -> String let full = 42 let empty: Int? = nil print("showValue(full) -> \(showValue(full))") print("showValue(empty) -> \(showValue(empty))") showValue(full) -> The value is: 42 showValue(empty) -> No value


Zig

In Zig, add ? before the type name like ?32 to make it 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("std"); fn showValue(allocator: std.mem.Allocator, opt: ?i32) ![]u8 pub fn main() !void showValue(allocator, full) -> The value is: 42 showValue(allocator, empty) -> No value


See also

* Result type * Tagged union * Nullable type * Null object pattern * Exception handling * Pattern matching


References

{{Data types Functional programming Data types Type theory