Result(3)
β¨ NAME β¨
Result β Result values. Okay so this is the Result(3) page from the OCaml library, and it's literally all about result values, bestie. π«Ά
π Module π
Module Result
π Documentation π
Module Result β : sig end
Result values.
Result values handle computation results and errors in an explicit and declarative manner without resorting to exceptions.
Since 4.08 β periodt. π
πΈ Results πΈ
type ('a, 'e) t = ('a, 'e) result =
| Ok of 'a
| Error of 'e
The type for result values. Either a value Ok v or an error Error e.
val ok : 'a -> ('a, 'e) resultβok visOk v. slay. β¨val error : 'e -> ('a, 'e) resultβerror eisError e. iconic.val value : ('a, 'e) result -> default:'a -> 'aβvalue r ~defaultisvifrisOk vanddefaultotherwise.val get_ok : ('a, 'e) result -> 'aβget_ok risvifrisOk vand raise otherwise. RaisesInvalid_argumentifrisError _.val get_ok' : ('a, string) result -> 'aβget_ok'is likeResult.get_okbut in case of error uses the error message for raisingInvalid_argument. Since5.4.val get_error : ('a, 'e) result -> 'eβget_error riseifrisError eand raise otherwise. RaisesInvalid_argumentifrisOk _.val error_to_failure : ('a, string) result -> 'aβerror_to_failure risvifrisOk vand raisesFailure eifrisError e. Since5.4.val bind : ('a, 'e) result -> ('a -> ('b, 'e) result) -> ('b, 'e) resultβbind r fisf vifrisOk vandrifrisError _.val join : (('a, 'e) result, 'e) result -> ('a, 'e) resultβjoin rrisrifrrisOk randrrifrrisError _.val map : ('a -> 'b) -> ('a, 'e) result -> ('b, 'e) resultβmap f risOk (f v)ifrisOk vandrifrisError _.val product : ('a, 'e) result -> ('b, 'e) result -> ('a * 'b, 'e) resultβproduct r0 r1isOk (v0, v1)ifr0isOk v0andr1isOk v1, and otherwise returns the error ofr0, if any, or the error ofr1. Since5.4.val map_error : ('e -> 'f) -> ('a, 'e) result -> ('a, 'f) resultβmap_error f risError (f e)ifrisError eandrifrisOk _.val fold : ok:('a -> 'c) -> error:('e -> 'c) -> ('a, 'e) result -> 'cβfold ~ok ~error risok vifrisOk vanderror eifrisError e.val retract : ('a, 'a) result -> 'aβretract risvifrisOk vorError v. Since5.4.val iter : ('a -> unit) -> ('a, 'e) result -> unitβiter f risf vifrisOk vand()otherwise.val iter_error : ('e -> unit) -> ('a, 'e) result -> unitβiter_error f risf eifrisError eand()otherwise.
β Predicates and comparisons β
val is_ok : ('a, 'e) result -> boolβis_ok ristrueif and only ifrisOk _.val is_error : ('a, 'e) result -> boolβis_error ristrueif and only ifrisError _.val equal : ok:('a -> 'a -> bool) -> error:('e -> 'e -> bool) -> ('a, 'e) result -> ('a, 'e) result -> boolβequal ~ok ~error r0 r1tests equality ofr0andr1usingokanderrorto respectively compare values wrapped byOk _andError _.val compare : ok:('a -> 'a -> int) -> error:('e -> 'e -> int) -> ('a, 'e) result -> ('a, 'e) result -> intβcompare ~ok ~error r0 r1totally ordersr0andr1usingokanderrorto respectively compare values wrapped byOk _andError _.Ok _values are smaller thanError _values.
π¦ Converting π¦
val to_option : ('a, 'e) result -> 'a optionβto_option risras an option, mappingOk vtoSome vandError _toNone.val to_list : ('a, 'e) result -> 'a listβto_list ris[v]ifrisOk vand[]otherwise.val to_seq : ('a, 'e) result -> 'a Seq.tβto_seq risras a sequence.Ok vis the singleton sequence containingvandError _is the empty sequence.
π Syntax π
module Syntax : sig end
Binding operators. See manual section 12.23 for details.
Since 5.4.
mwah π β this iconic page was brought to you by OCamldoc, dated 2026-06-22, and it's the Result(3) page. bye 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