Stdlib.Either(3)

πŸ’œ Stdlib.Either β€” the manual page, but make it bestie-friendly

okay so this is Stdlib.Either from the OCaml library man pages, and the official NAME section literally just says "no description" 😭 which is honestly the most mysterious main-character entrance ever. Anyway bestie, I got you β€” let's vibe check this thing together 🫢


🌸 NAME

Stdlib.Either β€” no description πŸ’€ (yes, fr, that's what it says, the docs said "figure it out" and left)


πŸŽ€ Module

The module is Stdlib.Either, and the documentation is for:

Module Either
 : (module Stdlib__Either)

Like, it's its own module, it's giving self-sufficient icon energy ✨


πŸ¦‹ Documentation

🌈 the type itself

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 personality β€” it's one or the other, never both, no cap.


πŸ’– the functions, one by one

  • val left : 'a -> ('a, 'b) t β€” left v is Left v. Simple. Iconic. Wrap that baby up on the left side πŸŽ€
  • val right : 'b -> ('a, 'b) t β€” right v is Right v. Same deal, but the right side this time. Balance ✨
  • val is_left : ('a, 'b) t -> bool β€” is_left (Left v) is true, and is_left (Right v) is false. A little vibe check for which side you're on πŸ‘‘
  • val is_right : ('a, 'b) t -> bool β€” is_right (Left v) is false, and is_right (Right v) is true. The mirror of the one above, obviously πŸ’…
  • val get_left : ('a, 'b) t -> 'a β€” get_left e is v if e is Left v, and raise otherwise 😭
    • Since 5.4
    • Raises Invalid_argument if e is Right _. So like, don't do that. For real. It will raise and you will be sad πŸ™ˆ
  • val get_right : ('a, 'b) t -> 'b β€” get_right e is v if e is Right v, and raise otherwise 😭
    • Since 5.4
    • Raises Invalid_argument if e is Left _. Same warning, other side. Respect the raise fr ⚠️
  • val find_left : ('a, 'b) t -> 'a option β€” find_left (Left v) is Some v, and find_left (Right _) is None. The safe, non-dramatic version 🧚
  • val find_right : ('a, 'b) t -> 'b option β€” find_right (Right v) is Some v, and find_right (Left _) is None. Matching set, slay πŸ’œ
  • 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 _. So it only transforms the left side and leaves the right side completely untouched, as it should πŸ”₯
  • 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 energy, other wing πŸ¦‹
  • 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). Two labelled functions, whichever side shows up gets transformed πŸ’«
  • 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. Both roads lead to the same type 'c, so you always get something back, periodt πŸ‘‘
  • 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 out either way β€” chef's kiss 🀌
    • 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. For when you just wanna do a lil side effect and return unit 🌸
  • 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. Returns a bool, obviously πŸ’…
  • 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 _. So you bring your own comparison functions for each side, and it 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 heads up bestie: Left _ values are smaller than Right _ values πŸ”₯ that's the tiebreaker baked right in, iykyk.

πŸ’€ friendly reminder that get_left and get_right will straight up raise Invalid_argument if you call them on the wrong side. Use find_left / find_right if you're not feeling brave today 🫢

πŸŽ€ cute lil sign-off

This page came from OCamldoc, dated 2026-06-22, and it's the Stdlib.Either(3) page from the OCaml library. That's the whole footer, bestie β€” consider it delivered with a bow on top πŸŽ€βœ¨πŸ’–

πŸ“¦ where this page comes from