Stdlib.Either
Either type.
Either is the simplest and most generic sum/variant type: a value of ('a, 'b) Either.t
is either a Left (v : 'a)
or a Right (v : 'b)
.
It is a 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.
For example:
List.partition_map:
('a -> ('b, 'c) Either.t) -> 'a list -> 'b list * 'c list
If you are looking for a parametrized type where one alternative means success and the other means failure, you should use the more specific type Result.t
.
A value of ('a, 'b) Either.t
contains either a value of 'a
or a value of 'b
let left: 'a => t('a, 'b);
left v
is Left v
.
let right: 'b => t('a, 'b);
right v
is Right v
.
let is_left: t('a, 'b) => bool;
is_left (Left v)
is true
, is_left (Right v)
is false
.
let is_right: t('a, 'b) => bool;
is_right (Left v)
is false
, is_right (Right v)
is true
.
let find_left: t('a, 'b) => option('a);
find_left (Left v)
is Some v
, find_left (Right _)
is None
let find_right: t('a, 'b) => option('b);
find_right (Right v)
is Some v
, find_right (Left _)
is None
map_left f e
is Left (f v)
if e
is Left v
and e
if e
is Right _
.
map_right f e
is Right (f v)
if e
is Right v
and e
if e
is Left _
.
map ~left ~right (Left v)
is Left (left v)
, map ~left ~right (Right v)
is Right (right v)
.
let fold: left:('a => 'c) => right:('b => 'c) => t('a, 'b) => 'c;
fold ~left ~right (Left v)
is left v
, and fold ~left ~right (Right v)
is right v
.
let iter: left:('a => unit) => right:('b => unit) => t('a, 'b) => unit;
iter ~left ~right (Left v)
is left v
, and iter ~left ~right (Right v)
is right v
.
let for_all: left:('a => bool) => right:('b => bool) => t('a, 'b) => bool;
for_all ~left ~right (Left v)
is left v
, and for_all ~left ~right (Right v)
is right v
.
equal ~left ~right e0 e1
tests equality of e0
and e1
using left
and right
to respectively compare values wrapped by Left _
and Right _
.