package base
Install
Dune Dependency
Authors
Maintainers
Sources
md5=2100b0ed13fecf43be86ed45c5b2cc4d
sha512=628610caff7e124631870fa1e29661caac28bdfdb18750ee43b868037da3d65d6dd9023b4be7c4c52405679efb5e865a6632d95606a22b28a36636a6bf706ef3
doc/base/Base/Option/index.html
Module Base.Option
Source
The option type indicates whether a meaningful value is present. It is frequently used to represent success or failure, using None
for failure. To be more descriptive about why a function failed, see the Or_error
module.
Usage example from a utop session follows. Hash table lookups use the option type to indicate success or failure when looking up a key.
# let h = Hashtbl.of_alist (module String) [ ("Bar", "Value") ];; val h : (string, string) Hashtbl.t = <abstr>;; - : (string, string) Hashtbl.t = <abstr> # Hashtbl.find h "Foo";; - : string option = None # Hashtbl.find h "Bar";; - : string option = Some "Value"
Type and Interfaces
include Invariant.S1 with type 'a t := 'a t
Applicative interface
Options form an applicative, where:
return x = Some x
None <*> x = None
Some f <*> None = None
Some f <*> Some x = Some (f x)
include Applicative.S_local with type 'a t := 'a t
Monadic interface
Options form a monad, where:
return x = Some x
(None >>= f) = None
(Some x >>= f) = f x
include Monad.S_local with type 'a t := 'a t
t >>= f
returns a computation that sequences the computations represented by two monad elements. The resulting computation first does t
to yield a value v
, and then runs the computation returned by f v
.
ignore_m t
is map t ~f:(fun _ -> ())
. ignore_m
used to be called ignore
, but we decided that was a bad name, because it shadowed the widely used Stdlib.ignore
. Some monads still do let ignore = ignore_m
for historical reasons.
Like all
, but ensures that every monadic value in the list produces a unit value, all of which are discarded rather than being collected into a list.
These are convenient to have in scope when programming with a monad:
Extracting Underlying Values
Extracts the underlying value if present, otherwise returns default
.
Extracts the underlying value, or raises if there is no value present. The error raised can be augmented using the ~here
, ~error
, and ~message
optional arguments.
Extracts the underlying value and applies f
to it if present, otherwise returns default
.
Extracts the underlying value if present, otherwise executes and returns the result of default
. default
is only executed if the underlying value is absent.
On None
, returns init
. On Some x
, returns f init x
.
Checks whether the provided element is there, using equal
.
find t ~f
returns t
if t = Some x
and f x = true
; otherwise, find
returns None
.
On None
, returns None
. On Some x
, returns f x
.
call x f
runs an optional function ~f
on the argument.
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
.
Constructors
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.
try_with_join f
returns the optional value returned by f
if it exits normally, and None
if f
raises an exception.
first_some t1 t2
returns t1
if it has an underlying value, or t2
otherwise.
some_if b x
converts a value x
to Some x
if b
, and None
otherwise.