module Option: sig
.. end
Option
wraps the output x
of successful functions in Some x
. Failed
functions return None
.
type 'a
t = 'a option
Options are preferred over exceptions. For example, use
let data = [(2, "two"); (5, "five"); (8, "eight")];;
let f x = match List.Assoc.find_opt x data with
| Some y -> y
| None -> "zero" (* where "zero" is some default value *);;
rather than
let f x = try List.Assoc.find x data with Not_found -> "zero";;
In this case using an exception is shorter, but in nontrivial code options
are easier to understand.
include Container.S1
include Monad.S
Options form a monad, where return x = Some x
,
(None >>= f) = None
, and (Some x >>= f) = f x
.
val is_none : 'a t -> bool
is_none t
returns true iff t = None.
val is_some : 'a t -> bool
is_some t
returns true iff t = Some x.
val value_map : 'a t -> default:'b -> f:('a -> 'b) -> 'b
value_map ~default ~f
is the same as function Some x -> f x | None -> default
val map2 : 'a t -> 'b t -> f:('a -> 'b -> 'c) -> 'c t
map2 o f
map 'a option and 'b option to a 'c option using ~f
val call : 'a -> f:('a -> unit) t -> unit
call x f
run optional function on argument
val apply : 'a -> f:('a -> 'b) t -> 'b t
apply x f
run optional function on argument and return an option
val value : 'a t -> default:'a -> 'a
value None ~default
= default
value (Some x) ~default
= x
val value_exn : ?here:Source_code_position0.t ->
?error:Error.t -> ?message:string -> 'a t -> 'a
value_exn (Some x)
= x
. value_exn None
raises an error whose contents contain
the supplied ~here
, ~error
, and message
, or a default message if none are
supplied.
val equal : ('a -> 'a -> bool) -> 'a t -> 'a t -> bool
val some : 'a -> 'a t
val both : 'a t -> 'b t -> ('a * 'b) t
val first_some : 'a t -> 'a t -> 'a t
val some_if : bool -> 'a -> 'a t
val merge : 'a t -> 'a t -> f:('a -> 'a -> 'a) -> 'a t
merge a b ~f
merges together the values from a
and b
using f
. If both a
and
b
are None
, returns None
. If only one is Some
, returns that one, and if both
are Some
, returns Some
of the result of applying f
to the contents of a
and
b
.
val filter : f:('a -> bool) -> 'a t -> 'a t
val try_with : (unit -> 'a) -> 'a t
try_with f
returns Some x
if f
returns x
and None
if f
raises an
exception. See Result.try_with
if you'd like to know which exception.
val compare : cmp:('a -> 'a -> int) -> 'a t -> 'a t -> int
val validate : none:unit Validate.check ->
some:'a Validate.check -> 'a t Validate.check
val t_of_sexp : (Sexplib.Sexp.t -> 'a) -> Sexplib.Sexp.t -> 'a t
val sexp_of_t : ('a -> Sexplib.Sexp.t) -> 'a t -> Sexplib.Sexp.t
val bin_t : 'a Bin_prot.Type_class.t -> 'a t Bin_prot.Type_class.t
val bin_read_t : 'a Bin_prot.Unsafe_read_c.reader -> 'a t Bin_prot.Read_ml.reader
val bin_read_t_ : 'a Bin_prot.Unsafe_read_c.reader -> 'a t Bin_prot.Unsafe_read_c.reader
val bin_read_t__ : 'a Bin_prot.Unsafe_read_c.reader ->
(int -> 'a t) Bin_prot.Unsafe_read_c.reader
val bin_reader_t : 'a Bin_prot.Type_class.reader -> 'a t Bin_prot.Type_class.reader
val bin_size_t : 'a Bin_prot.Size.sizer -> 'a t Bin_prot.Size.sizer
val bin_write_t : 'a Bin_prot.Unsafe_write_c.writer -> 'a t Bin_prot.Write_ml.writer
val bin_write_t_ : 'a Bin_prot.Unsafe_write_c.writer ->
'a t Bin_prot.Unsafe_write_c.writer
val bin_writer_t : 'a Bin_prot.Type_class.writer -> 'a t Bin_prot.Type_class.writer
Options form a monad, where return x = Some x
,
(None >>= f) = None
, and (Some x >>= f) = f x
.
is_none t
returns true iff t = None.
is_some t
returns true iff t = Some x.
value_map ~default ~f
is the same as function Some x -> f x | None -> default
map2 o f
map 'a option and 'b option to a 'c option using ~f
call x f
run optional function on argument
apply x f
run optional function on argument and return an option
value None ~default
= default
value (Some x) ~default
= x
value_exn (Some x)
= x
. value_exn None
raises an error whose contents contain
the supplied ~here
, ~error
, and message
, or a default message if none are
supplied.
merge a b ~f
merges together the values from a
and b
using f
. If both a
and
b
are None
, returns None
. If only one is Some
, returns that one, and if both
are Some
, returns Some
of the result of applying f
to the contents of a
and
b
.
try_with f
returns Some x
if f
returns x
and None
if f
raises an
exception. See Result.try_with
if you'd like to know which exception.