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. π
β οΈ RaisesInvalid_argumentifoisNone. so like, don't call it on aNoneunless 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 Noneistrue,for_all p (Some v)isp 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 Noneisfalse,exists p (Some v)isp 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
- arch rolling: ocaml 5.5.0-1 Β· LGPL2.1 AND custom: QPL-1.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms