Option
Edit on GitHubUtilities for working with the Option data type.
The Option
type is a special type of enum that exists to represent the possibility of something being present (with the Some
variant), or not (with the None
variant). There’s no standalone null
or none
type in Grain; use an Option
where you would normally reach for null
or none
.
Create new Option
values using Some()
and None
.
Values
Option.isSome
Checks if the Option is the Some
variant.
Option.isNone
Checks if the Option is the None
variant.
Option.contains
Checks if the Option is the Some
variant and contains the given value. Uses the generic ==
equality operator.
Option.expect
Attempts to unwrap the inner value from the Some
variant. If it is None
, raises Failure
with the provided message.
Option.unwrap
Attempts to unwrap the inner value from the Some
variant. If it is None
, raises Failure
with a generic message.
Option.unwrapWithDefault
Unwraps the inner value from the Some
variant. If it is None
, returns the default value provided.
Option.map
If the Option is the Some
variant, call fn
with the inner value and returns a new Some
variant with the produced value.
Option.mapWithDefault
If the Option is the Some
variant, call fn
with the inner value and returns the result. If it is None
, returns the default value provided.
Option.mapWithDefaultFn
If the Option is the Some
variant, call fn
with the inner value and returns the result. If it is None
, returns the result of the default function provided.
Option.flatMap
If the Option is the Some
variant, call fn
with the inner value. The fn
must produce its own Option to be returned.
Option.filter
If the Option is the Some
variant, call fn
with the inner value. If the fn
returns false
, returns a None
variant type.
Option.zip
If both Options are the Some
variant, returns a new Some
variant that contains a tuple of both values. If any of the Options are None
, returns None
.
Option.zipWith
If both Options are the Some
variant, call fn
with both inner values and returns a new Some
variant with the produced value. If any of the Options are None
, returns None
.
Option.flatten
Flattens nested Options, like Some(Some(1))
to Some(1)
. If any of the Options are None
, returns None
.
Option.toList
If the Option is the Some
variant, returns a List
containing the inner value as the only item. If it is None
, returns an empty List
.
Option.toArray
If the Option is the Some
variant, returns an Array
containing the inner value as the only item. If it is None
, returns an empty Array
.
Option.toResult
If the Option is the Some(a)
, returns Ok(a)
. If it is None
, returns an Err
of the provided error value.
Option.sideEffect
If the Option is the Some
variant, call fn
with the inner value. Always returns void
.
Option.peek
If the Option is the Some
variant, call fn
with the inner value. Always returns the Option it is called with; this method is a “chainable” Option.sideEffect
.
Option.and
If the first Option is the Some
variant, returns the second Option. Returns None
otherwise.
Option.or
Returns the first Option if it is the Some
variant. Returns the second Option otherwise.