Skip to content

Module Js.Null

Utility functions on null

Provides functionality for dealing with the 'a Js.nullJs.null('a) type

ocaml
type +'a t = 'a Js.null
reasonml
type t(+'a) = Js.null('a);

Local alias for 'a Js.nullJs.null('a)

ocaml
val return : 'a -> 'a t
reasonml
let return: 'a => t('a);

Constructs a value of 'a Js.nullJs.null('a) containing a value of 'a

ocaml
val empty : 'a t
reasonml
let empty: t('a);

The empty value, null

ocaml
val getUnsafe : 'a t -> 'a
reasonml
let getUnsafe: t('a) => 'a;
ocaml
val getExn : 'a t -> 'a
reasonml
let getExn: t('a) => 'a;
ocaml
val bind : f:('a -> 'b t) Js.Fn.arity1 -> 'a t -> 'b t
reasonml
let bind: f:Js.Fn.arity1(('a => t('b))) => t('a) => t('b);
ocaml
val map : f:('a -> 'b) Js.Fn.arity1 -> 'a t -> 'b t
reasonml
let map: f:Js.Fn.arity1(('a => 'b)) => t('a) => t('b);

Maps the contained value using the given function

If 'a Js.nullJs.null('a) contains a value, that value is unwrapped, mapped to a 'b using the given function a' -> 'ba' => 'b, then wrapped back up and returned as 'b Js.nullJs.null('b)

ocaml
let maybeGreetWorld (maybeGreeting: string Js.null) =
  Js.Null.bind maybeGreeting ~f:(fun greeting -> greeting ^ " world!")
reasonml
let maybeGreetWorld = (maybeGreeting: Js.null(string)) =>
  Js.Null.bind(maybeGreeting, ~f=greeting => greeting ++ " world!");
ocaml
val iter : f:('a -> unit) Js.Fn.arity1 -> 'a t -> unit
reasonml
let iter: f:Js.Fn.arity1(('a => unit)) => t('a) => unit;

Iterates over the contained value with the given function

If 'a Js.nullJs.null('a) contains a value, that value is unwrapped and applied to the given function.

ocaml
let maybeSay (maybeMessage: string Js.null) =
  Js.Null.iter maybeMessage ~f:(fun message -> Js.log message)
reasonml
let maybeSay = (maybeMessage: Js.null(string)) =>
  Js.Null.iter(maybeMessage, ~f=message => Js.log(message));
ocaml
val fromOption : 'a option -> 'a t
reasonml
let fromOption: option('a) => t('a);

Maps 'a optionoption('a) to 'a Js.nullJs.null('a)

Some areturn a
Noneempty
ocaml
val toOption : 'a t -> 'a option
reasonml
let toOption: t('a) => option('a);

Maps 'a Js.nullJs.null('a) to 'a optionoption('a)

return aSome a
emptyNone