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 type Result.t instead. Either is 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 v is Left v. simple as that, no notes πŸ‘‘
  • val right : 'b -> ('a, 'b) t β€” right v is Right v. the other half of the duo πŸ’œ
  • val is_left : ('a, 'b) t -> bool β€” is_left (Left v) is true, is_left (Right v) is false. a vibe check for your Lefts.
  • val is_right : ('a, 'b) t -> bool β€” is_right (Left v) is false, is_right (Right v) is true. the mirror image, we love a symmetric icon πŸŽ€
  • val get_left : ('a, 'b) t -> 'a β€” get_left e is v if e is Left v, and raises otherwise.

Since 5.4

😭 Raises Invalid_argument if e is Right _. 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 e is v if e is Right v, and raises otherwise.

Since 5.4

😭 Raises Invalid_argument if e is Left _. same energy, opposite side.
  • val find_left : ('a, 'b) t -> 'a option β€” find_left (Left v) is Some v, find_left (Right _) is None. 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) is Some v, find_right (Left _) is None. matching set, obsessed 🫢
  • val map_left : ('a1 -> 'a2) -> ('a1, 'b) t -> ('a2, 'b) t β€” map_left f e is Left (f v) if e is Left v, and e if e is Right _. 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 e is Right (f v) if e is Right v, and e if e is Left _. same deal, other side πŸ’œ
  • val map : left:('a1 -> 'a2) -> right:('b1 -> 'b2) -> ('a1, 'b1) t -> ('a2, 'b2) t β€” map ~left ~right (Left v) is Left (left v), and map ~left ~right (Right v) is Right (right v). both sides handled, no favorites πŸ”₯
  • val fold : left:('a -> 'c) -> right:('b -> 'c) -> ('a, 'b) t -> 'c β€” fold ~left ~right (Left v) is left v, and fold ~left ~right (Right v) is right v. squish it down to one thing, either way 🌸
  • val retract : ('a, 'a) t -> 'a β€” retract (Left v) is v, and retract (Right v) is v. 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) is left v, and iter ~left ~right (Right v) is right 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) is left v, and for_all ~left ~right (Right v) is right 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 e1 tests equality of e0 and e1 using left and right to respectively compare values wrapped by Left _ and Right _. 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 e1 totally orders e0 and e1 using left and right to respectively compare values wrapped by Left _ and Right _.
πŸ‘‘ and here's the rule, don't forget it: Left _ values are smaller than Right _ 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