Option(3)

πŸ’– Option(3) β€” the OCaml library page, but make it make sense πŸ’–

okay bestie, so this is the Option(3) page from the OCaml library manual, and honestly? It's kind of iconic. Let's get into it 🫢


🌸 NAME 🌸

Option β€” option values. that's it. that's the name. periodt.


πŸŽ€ Module πŸŽ€

Module Option


✨ Documentation ✨

Module Option : sig end

Option values, bestie!! πŸ’…

Option values explicitly indicate the presence or absence of a value. Like, it's giving "is there something here or not" energy. no cap.

Since 4.08


πŸ¦‹ Options πŸ¦‹

type 'a t = 'a option =
 | None
 | Some of 'a

This is the type for option values. It's literally either None or a value Some v. that's the whole vibe. 🌈


  • val none : 'a option

none is None. that's it. queen of doing nothing and doing it flawlessly. πŸ’€

  • val some : 'a -> 'a option

some v is Some v. yes bestie, it wraps your value up all cute. 🎁

  • val value : 'a option -> default:'a -> 'a

value o ~default is v if o is Some v, and default otherwise. so like, it's the fallback friend. 🫢

  • val get : 'a option -> 'a

get o is v if o is Some v, and raises otherwise. 😭

⚠️ Raises Invalid_argument if o is None. so like, don't call it on a None unless you want chaos. i'm serious. πŸ’€
  • val bind : 'a option -> ('a -> 'b option) -> 'b option

bind o f is f v if o is Some v, and None if o is None. chaining era. πŸ”₯

  • val join : 'a option option -> 'a option

join oo is Some v if oo is Some (Some v), and None otherwise. it un-nests. very demure, very mindful. ✨

  • val map : ('a -> 'b) -> 'a option -> 'b option

map f o is None if o is None, and Some (f v) if o is Some v. slay, transform the contents. πŸ’…

  • val product : 'a option -> 'b option -> ('a * 'b) option

product o0 o1 is Some (v0, v1) if o0 is Some v0 and o1 is Some v1, and None otherwise. ship them together. πŸ’•

Since 5.5

  • val fold : none:'a -> some:('b -> 'a) -> 'b option -> 'a

fold ~none ~some o is none if o is None, and some v if o is Some v. two paths, one destiny. πŸ›€οΈ

  • val iter : ('a -> unit) -> 'a option -> unit

iter f o is f v if o is Some v, and () otherwise. side effects, but only when there's something there. πŸ“

  • val blend : ('a -> 'a -> 'a) -> 'a option -> 'a option -> 'a option

blend f o1 o2 is o1 if o2 is None, o2 if o1 is None, and Some (f v1 v2) if o1 is Some v1 and o2 is Some v2. mix them together bestie. 🧚

Since 5.5

  • val for_all : ('a -> bool) -> 'a option -> bool

for_all p behaves like List.for_all p on a list of zero or one element:

  • for_all p None is true,
  • for_all p (Some v) is p v.

Since 5.5

  • val exists : ('a -> bool) -> 'a option -> bool

exists p behaves like List.exists p on a list of zero or one element:

  • exists p None is false,
  • exists p (Some v) is p v.

Since 5.5


πŸ‘‘ Predicates and comparisons πŸ‘‘

vibe check section, let's go πŸ’œ

  • val is_none : 'a option -> bool

is_none o is true if and only if o is None. simple. gorgeous. stunning. ✨

  • val is_some : 'a option -> bool

is_some o is true if and only if o is Some o. literally the check you'll use every day, iykyk. πŸ’…

  • val equal : ('a -> 'a -> bool) -> 'a option -> 'a option -> bool

equal eq o0 o1 is true if and only if o0 and o1 are both None, or if they are Some v0 and Some v1 and eq v0 v1 is true. matched energy. 🫢

  • val compare : ('a -> 'a -> int) -> 'a option -> 'a option -> int

compare cmp o0 o1 is a total order on options, using cmp to compare values wrapped by Some _. and important: None is smaller than Some _ values. no cap, that's the rule. ⭐


🌈 Converting 🌈

the glow-up section πŸ’–

  • val to_result : none:'e -> 'a option -> ('a, 'e) result

to_result ~none o is Ok v if o is Some v, and Error none otherwise. option to result, iconic transition. πŸ”₯

  • val to_list : 'a option -> 'a list

to_list o is [] if o is None, and [v] if o is Some v. zero or one, never messy. πŸŽ€

  • val to_seq : 'a option -> 'a Seq.t

to_seq o is o as a sequence. None is the empty sequence, and Some v is the singleton sequence containing v. sequence era. πŸ¦‹


🧚 Syntax 🧚

module Syntax : sig end

Binding operators, bestie!! see manual section 12.23 for the details πŸ‘€

Since 5.5


anyway that's the whole page, you ate that πŸ’…


~ cute sign-off ~ this little masterpiece was brought to you by OCamldoc, dated 2026-06-22, and it's the Option(3) page. mwah πŸ’‹ go write some Some and None and be gorgeous about it 🌸🫢

πŸ“¦ where this page comes from