Either(3)
β¨ Either β¨
okay so bestie, welcome to Either β the OCaml library page for the Either type π
this is <italic>Either</italic>(3), and yes we're doing the whole thing, no skimming, no cap.
π NAME
Either β Either type. that's it, that's the name. iconic behavior honestly π«Ά
πΈ Module
Module Either
π Documentation
Module Either : sig end
Either type. periodt.
Either is literally the simplest and most generic sum/variant type out there: a value of ('a, 'b) Either.t is either a Left (v : 'a) or a Right (v : 'b). two cases, no drama, no favorites π
It's the natural choice in the API of generic functions where values could fall in two different cases β possibly at different types β without assigning a specific meaning to what each case should be. like, it's giving "two options, you decide what they mean." main character energy fr fr π
For example:
List.partition_map:
('a -> ('b, 'c) Either.t) -> 'a list -> 'b list * 'c list
π¨ important vibe check: if you're looking for a parametrized type where one alternative means success and the other means failure, you should use the more specific typeResult.tinstead.Eitheris not that girl, she has no opinions about success or failure. iykyk β¨
Since 4.12 β
type ('a, 'b) t =
| Left of 'a
| Right of 'b
A value of ('a, 'b) Either.t contains either a value of 'a or a value of 'b. that's the whole concept bestie, we ate π
π¦ the functions
val left : 'a -> ('a, 'b) tβleft visLeft v. simple as that, no notes πval right : 'b -> ('a, 'b) tβright visRight v. the other half of the duo πval is_left : ('a, 'b) t -> boolβis_left (Left v)istrue,is_left (Right v)isfalse. a vibe check for your Lefts.val is_right : ('a, 'b) t -> boolβis_right (Left v)isfalse,is_right (Right v)istrue. the mirror image, we love a symmetric icon πval get_left : ('a, 'b) t -> 'aβget_left eisvifeisLeft v, and raises otherwise.
Since 5.4
π RaisesInvalid_argumentifeisRight _. so like, only call it if you're sure bestie, or she WILL throw hands (an exception).
val get_right : ('a, 'b) t -> 'bβget_right eisvifeisRight v, and raises otherwise.
Since 5.4
π RaisesInvalid_argumentifeisLeft _. same energy, opposite side.
val find_left : ('a, 'b) t -> 'a optionβfind_left (Left v)isSome v,find_left (Right _)isNone. the chill, non-exploding version, we stan πval find_right : ('b, 'a) t -> 'b optionβ wait, no, let's be exact:val find_right : ('a, 'b) t -> 'b option.find_right (Right v)isSome v,find_right (Left _)isNone. matching set, obsessed π«Άval map_left : ('a1 -> 'a2) -> ('a1, 'b) t -> ('a2, 'b) tβmap_left f eisLeft (f v)ifeisLeft v, andeifeisRight _. transforms the left, leaves the right untouched, she minds her business β¨val map_right : ('b1 -> 'b2) -> ('a, 'b1) t -> ('a, 'b2) tβmap_right f eisRight (f v)ifeisRight v, andeifeisLeft _. same deal, other side πval map : left:('a1 -> 'a2) -> right:('b1 -> 'b2) -> ('a1, 'b1) t -> ('a2, 'b2) tβmap ~left ~right (Left v)isLeft (left v), andmap ~left ~right (Right v)isRight (right v). both sides handled, no favorites π₯val fold : left:('a -> 'c) -> right:('b -> 'c) -> ('a, 'b) t -> 'cβfold ~left ~right (Left v)isleft v, andfold ~left ~right (Right v)isright v. squish it down to one thing, either way πΈval retract : ('a, 'a) t -> 'aβretract (Left v)isv, andretract (Right v)isv. when both sides are the same type you just... get the value. so clean.
Since 5.4 β
val iter : left:('a -> unit) -> right:('b -> unit) -> ('a, 'b) t -> unitβiter ~left ~right (Left v)isleft v, anditer ~left ~right (Right v)isright v. side effects only, no return value, we love a lowkey function πval for_all : left:('a -> bool) -> right:('b -> bool) -> ('a, 'b) t -> boolβfor_all ~left ~right (Left v)isleft v, andfor_all ~left ~right (Right v)isright v. a yes/no on whichever side is holding the value πval equal : left:('a -> 'a -> bool) -> right:('b -> 'b -> bool) -> ('a, 'b) t -> ('a, 'b) t -> boolβequal ~left ~right e0 e1tests equality ofe0ande1usingleftandrightto respectively compare values wrapped byLeft _andRight _. you bring the comparison functions, she does the rest π«Άval compare : left:('a -> 'a -> int) -> right:('b -> 'b -> int) -> ('a, 'b) t -> ('a, 'b) t -> intβcompare ~left ~right e0 e1totally orderse0ande1usingleftandrightto respectively compare values wrapped byLeft _andRight _.
π and here's the rule, don't forget it:Left _values are smaller thanRight _values. periodt.
and that's the whole page bestie, every function, every signature, nothing skipped, we ate and left no crumbs πβ¨
mwah π β this has been Either(3) from the OCaml library, brought to you by OCamldoc, dated 2026-06-22. go forth and sum your types π
π¦ 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