package core

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type

This module extends Base.Option with bin_io, quickcheck, and support for ppx_optional.

type 'a t = 'a Base.Option.t
include Bin_prot.Binable.S1 with type 'a t := 'a t
val bin_shape_t : Bin_prot.Shape.t -> Bin_prot.Shape.t
val bin_size_t : ('a, 'a t) Bin_prot.Size.sizer1
val bin_write_t : ('a, 'a t) Bin_prot.Write.writer1
val bin_read_t : ('a, 'a t) Bin_prot.Read.reader1
val __bin_read_t__ : ('a, int -> 'a t) Bin_prot.Read.reader1
val bin_writer_t : ('a, 'a t) Bin_prot.Type_class.S1.writer
val bin_reader_t : ('a, 'a t) Bin_prot.Type_class.S1.reader
val bin_t : ('a, 'a t) Bin_prot.Type_class.S1.t

Type and Interfaces

val compare : ('a -> 'a -> int) -> 'a option -> 'a option -> int
val globalize : ('a -> 'a) -> 'a option -> 'a option
val hash_fold_t : (Base.Hash.state -> 'a -> Base.Hash.state) -> Base.Hash.state -> 'a option -> Base.Hash.state
val t_sexp_grammar : 'a Sexplib0.Sexp_grammar.t -> 'a option Sexplib0.Sexp_grammar.t
include Base.Equal.S1 with type 'a t := 'a option
val equal : 'a Base.Equal.equal -> 'a option Base.Equal.equal
include Base.Invariant.S1 with type 'a t := 'a option
val invariant : ('a -> unit) -> 'a option -> unit
include Base.Sexpable.S1 with type 'a t := 'a option
val t_of_sexp : (Sexplib0.Sexp.t -> 'a) -> Sexplib0.Sexp.t -> 'a option
val sexp_of_t : ('a -> Sexplib0.Sexp.t) -> 'a option -> Sexplib0.Sexp.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 Base.Applicative.S_local with type 'a t := 'a option
val both : 'a option -> 'b option -> ('a * 'b) option
val (<*>) : ('a -> 'b) option -> 'a option -> 'b option

same as apply

val (<*) : 'a option -> unit option -> 'a option
val (*>) : unit option -> 'a option -> 'a option
val apply : ('a -> 'b) option -> 'a option -> 'b option
val map2 : 'a option -> 'b option -> f:('a -> 'b -> 'c) -> 'c option
val map3 : 'a option -> 'b option -> 'c option -> f:('a -> 'b -> 'c -> 'd) -> 'd option
module Applicative_infix = Base.Option.Applicative_infix

Monadic interface

Options form a monad, where:

  • return x = Some x
  • (None >>= f) = None
  • (Some x >>= f) = f x
include Base.Monad.S_local with type 'a t := 'a option
val (>>=) : 'a option -> ('a -> 'b option) -> 'b option

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.

val (>>|) : 'a option -> ('a -> 'b) -> 'b option

t >>| f is t >>= (fun a -> return (f a)).

module Monad_infix = Base.Option.Monad_infix
val bind : 'a option -> f:('a -> 'b option) -> 'b option

bind t ~f = t >>= f

val return : 'a -> 'a option

return v returns the (trivial) computation that returns v.

val map : 'a option -> f:('a -> 'b) -> 'b option

map t ~f is t >>| f.

val join : 'a option option -> 'a option

join t is t >>= (fun t' -> t').

val ignore_m : 'a option -> unit option

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.

val all : 'a option list -> 'a list option
val all_unit : unit option list -> unit option

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.

module Let_syntax = Base.Option.Let_syntax

Extracting Underlying Values

val value : 'a option -> default:'a -> 'a

Extracts the underlying value if present, otherwise returns default.

val value_exn : ?here:Lexing.position -> ?error:Base.Error.t -> ?message:string -> 'a option -> 'a

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.

val value_map : 'a option -> default:'b -> f:('a -> 'b) -> 'b

Extracts the underlying value and applies f to it if present, otherwise returns default.

val value_or_thunk : 'a option -> default:(unit -> 'a) -> 'a

Extracts the underlying value if present, otherwise executes and returns the result of default. default is only executed if the underlying value is absent.

val fold : 'a option -> init:'acc -> f:('acc -> 'a -> 'acc) -> 'acc

On None, returns init. On Some x, returns f init x.

val mem : 'a option -> 'a -> equal:('a -> 'a -> bool) -> bool

Checks whether the provided element is there, using equal.

val length : 'a option -> int
val iter : 'a option -> f:('a -> unit) -> unit
val exists : 'a option -> f:('a -> bool) -> bool

On None, returns false. On Some x, returns f x.

val for_all : 'a option -> f:('a -> bool) -> bool

On None, returns true. On Some x, returns f x.

val find : 'a option -> f:('a -> bool) -> 'a option

find t ~f returns t if t = Some x and f x = true; otherwise, find returns None.

val find_map : 'a option -> f:('a -> 'b option) -> 'b option

On None, returns None. On Some x, returns f x.

val to_list : 'a option -> 'a list
val to_array : 'a option -> 'a array
val call : 'a -> f:('a -> unit) option -> unit

call x f runs an optional function ~f on the argument.

val merge : 'a option -> 'a option -> f:('a -> 'a -> 'a) -> 'a option

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 : 'a option -> f:('a -> bool) -> 'a option

Constructors

val try_with : (unit -> 'a) -> 'a option

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 try_with_join : (unit -> 'a option) -> 'a option

try_with_join f returns the optional value returned by f if it exits normally, and None if f raises an exception.

val some : 'a -> 'a option

Wraps the Some constructor as a function.

val first_some : 'a option -> 'a option -> 'a option

first_some t1 t2 returns t1 if it has an underlying value, or t2 otherwise.

val some_if : bool -> 'a -> 'a option

some_if b x converts a value x to Some x if b, and None otherwise.

Predicates

val is_none : 'a option -> bool

is_none t returns true iff t = None.

val is_some : 'a option -> bool

is_some t returns true iff t = Some x.

include Comparator.Derived with type 'a t := 'a t
type !'cmp comparator_witness
include Quickcheckable.S1 with type 'a t := 'a t
val quickcheck_generator : 'a Base_quickcheck.Generator.t -> 'a t Base_quickcheck.Generator.t
val quickcheck_observer : 'a Base_quickcheck.Observer.t -> 'a t Base_quickcheck.Observer.t
val quickcheck_shrinker : 'a Base_quickcheck.Shrinker.t -> 'a t Base_quickcheck.Shrinker.t
val validate : none:unit Validate.check -> some:'a Validate.check -> 'a t Validate.check
module Stable : sig ... end
module Optional_syntax : Optional_syntax.S1 with type 'a t := 'a t and type 'a value := 'a

You might think that it's pointless to have Optional_syntax on options because OCaml already has nice syntax for matching on options. The reason to have this here is that you might have, for example, a tuple of an option and some other type that supports Optional_syntax. Since Optional_syntax can only be opted into at the granularity of the whole match expression, we need this Optional_syntax support for options in order to use it for the other half of the tuple.

OCaml

Innovation. Community. Security.