Null
t
type t<'a> = Js.Null.t<'a>
A type representing a value that can be either 'a
or null
.
asNullable
let asNullable: t<'a> => Core__Nullable.t<'a>
Converts a Null.t
into a Nullable.t
.
Examples
RESCRIPTlet nullValue = Null.make("Hello")
let asNullable = nullValue->Null.asNullable // Nullable.t<string>
null
let null: t<'a>
make
let make: 'a => t<'a>
Creates a new Null.t
from the provided value.
This means the compiler will enforce null checks for the new value.
Examples
RESCRIPTlet myStr = "Hello"
let asNullValue = myStr->Null.make // The compiler now thinks this can be `string` or `null`.
equal
let equal: (t<'a>, t<'b>, ('a, 'b) => bool) => bool
compare
let compare: (
t<'a>,
t<'b>,
('a, 'b) => Core__Ordering.t,
) => Core__Ordering.t
toOption
let toOption: t<'a> => option<'a>
Converts a nullable value into an option, so it can be pattern matched on.
Will convert null
to None
, and a present value to Some(value)
.
Examples
RESCRIPTlet nullStr = Null.make("Hello")
switch nullStr->Null.toOption {
| Some(str) => Console.log2("Got string:", str)
| None => Console.log("Didn't have a value.")
}
fromOption
let fromOption: option<'a> => t<'a>
Turns an option
into a Null.t
. None
will be converted to null
.
Examples
RESCRIPTlet optString: option<string> = None
let asNull = optString->Null.fromOption // Null.t<string>
Console.log(asNull == null) // Logs `true` to the console.
getOr
let getOr: (t<'a>, 'a) => 'a
getOr(value, default)
returns value
if not null
, otherwise return
default
.
Examples
RESCRIPTNull.getOr(null, "Banana") // Banana
Null.getOr(Nulalble.make("Apple"), "Banana") // Apple
let greet = (firstName: option<string>) =>
"Greetings " ++ firstName->Null.getOr("Anonymous")
Null.make("Jane")->greet // "Greetings Jane"
null->greet // "Greetings Anonymous"
getWithDefault
Deprecated
Use getOr instead
let getWithDefault: (t<'a>, 'a) => 'a
getExn
let getExn: t<'a> => 'a
getExn(value)
raises an exception if null
, otherwise returns the value.
RESCRIPTNull.getExn(Null.make(3)) // 3
Null.getExn(null) /* Raises an Error */
Exceptions
Raises
Invalid_argument
ifvalue
isnull
,
getUnsafe
let getUnsafe: t<'a> => 'a
getUnsafe(value)
returns value
.
Examples
RESCRIPTNull.getUnsafe(Null.make(3)) == 3
Null.getUnsafe(null) // Raises an error
Important
This is an unsafe operation, it assumes
value
is notnull
.
forEach
let forEach: (t<'a>, 'a => unit) => unit
forEach(value, f)
call f
on value
. if value
is not null
, then if calls
f
, otherwise returns unit
.
Examples
RESCRIPTNull.forEach(Null.make("thing"), x => Console.log(x)) // logs "thing"
Null.forEach(null, x => Console.log(x)) // returns ()
map
let map: (t<'a>, 'a => 'b) => t<'b>
map(value, f)
returns f(value)
if value
is not null
, otherwise returns
value
unchanged.
Examples
RESCRIPTNull.map(Null.make(3), x => x * x) // Null.make(9)
Null.map(null, x => x * x) // null
mapOr
let mapOr: (t<'a>, 'b, 'a => 'b) => 'b
mapOr(value, default, f)
returns f(value)
if value
is not null
,
otherwise returns default
.
Examples
RESCRIPTlet someValue = Null.make(3)
someValue->Null.mapOr(0, x => x + 5) // 8
let noneValue = null
noneValue->Null.mapOr(0, x => x + 5) // 0
mapWithDefault
Deprecated
Use mapOr instead
let mapWithDefault: (t<'a>, 'b, 'a => 'b) => 'b
flatMap
let flatMap: (t<'a>, 'a => t<'b>) => t<'b>
flatMap(value, f)
returns f(value)
if value
is not null
, otherwise
returns value
unchanged.
Examples
RESCRIPTlet addIfAboveOne = value =>
if (value > 1) {
Null.make(value + 1)
} else {
null
}
Null.flatMap(Null.make(2), addIfAboveOne) // Null.make(3)
Null.flatMap(Null.make(-4), addIfAboveOne) // null
Null.flatMap(null, addIfAboveOne) // null