Stdlib.Option(3)
π NAME π
okay bestie, so this page is Stdlib.Option, and it's comin' at you straight from the OCaml library manual, which is the whole vibe of this doc. ngl the original NAME section literally just says "no description" π so like... that's the description. we move.
πΈ Module πΈ
the module is Stdlib.Option. that's it, that's the line. iconic minimalism.
β¨ Documentation β¨
so we're chillin' in Module Option, and it's officially:
(module Stdlib__Option)
lowkey just a re-export situation, but slay, let's get into it π«Ά
π Options π
first up, the actual type, the main character:
type 'a t = 'a option =
| None
| Some of 'a
this is the type for option values β fr fr it's either None, or a value wrapped up as Some v. two moods, no in-between π
val none : 'a optionβnoneis literallyNone. that's the whole job. queen of doing nothing β¨val some : 'a -> 'a optionβsome vgives youSome v. wrap it up, we're done πval value : 'a option -> default:'a -> 'aβvalue o ~defaultisvwhenoisSome v, and it'sdefaultotherwise. the ultimate fallback bestie π«Άval get : 'a option -> 'aβget oisvwhenoisSome v, and it raises otherwise.
π¨ heads up bestie: this one raisesInvalid_argumentifoisNone. no cap, it will NOT be chill about it π
val bind : 'a option -> ('a -> 'b option) -> 'b optionβbind o fisf vifoisSome v, andNoneifoisNone. chaining era π«val join : 'a option option -> 'a optionβjoin ooisSome vifooisSome (Some v), andNoneotherwise. un-nesting the nesting, we love a flatten π¦val map : ('a -> 'b) -> 'a option -> 'b optionβmap f oisNoneifoisNone, andSome (f v)ifoisSome v. glow-up transformation sequence β¨val product : 'a option -> 'b option -> ('a * 'b) optionβproduct o0 o1isSome (v0, v1)ifo0isSome v0ando1isSome v1, andNoneotherwise. Since 5.5 βval fold : none:'a -> some:('b -> 'a) -> 'b option -> 'aβfold ~none ~some oisnoneifoisNone, andsome vifoisSome v. pick your fighter based on the vibe πval iter : ('a -> unit) -> 'a option -> unitβiter f oisf vifoisSome v, and()otherwise. do the thing, or don't, no pressure πval blend : ('a -> 'a -> 'a) -> 'a option -> 'a option -> 'a optionβblend f o1 o2iso1ifo2isNone, it'so2ifo1isNone, and it'sSome (f v1 v2)ifo1isSome v1ando2isSome v2. merging energies, we're obsessed π₯ Since 5.5 βval for_all : ('a -> bool) -> 'a option -> boolβfor_all pbehaves likeList.for_all pon 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 pbehaves likeList.exists pon a list of zero or one element:exists p Noneisfalse,exists p (Some v)isp v.
Since 5.5 β
π¦ Predicates and comparisons π¦
the "is it or isn't it" era, plus ranking your options, literally:
val is_none : 'a option -> boolβis_none oistrueif and only ifoisNone. vibe check: empty βval is_some : 'a option -> boolβis_some oistrueif and only ifoisSome o. vibe check: occupied βval equal : ('a -> 'a -> bool) -> 'a option -> 'a option -> boolβequal eq o0 o1istrueif and only ifo0ando1are bothNone, or if they'reSome v0andSome v1andeq v0 v1istrue. twin flames or nothin' πval compare : ('a -> 'a -> int) -> 'a option -> 'a option -> intβcompare cmp o0 o1is a total order on options, usingcmpto compare the values wrapped bySome _. and bestie,Noneis smaller thanSome _values. ranking rules, periodt π
π Converting π
glow-ups into other types, no cap:
val to_result : none:'e -> 'a option -> ('a, 'e) resultβto_result ~none oisOk vifoisSome v, andError noneotherwise. switching lanes to result-land πval to_list : 'a option -> 'a listβto_list ois[]ifoisNone, and[v]ifoisSome v. zero or one bestie, that's the list πval to_seq : 'a option -> 'a Seq.tβto_seq oisoas a sequence.Noneis the empty sequence, andSome vis the singleton sequence containingv. stream it π
π Syntax π
module Syntax : sig end
binding operators, bestie! see manual section 12.23 for the deets.
Since 5.5 β
π§ and that's the whole page, no notes. every flag, every val, every "Since 5.5", every raise β all present and accounted for π
cute little sign-off: this masterpiece was brought to you by OCamldoc, dated 2026-06-22, and it lives at Stdlib.Option(3). mwah π slay and read your manual pages, bestie πβ¨
π¦ 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