An Option represents a value that may or may not be present:
some(value)— a value is presentnone()— no value
Pattern matching on either form requires the parentheses. The runtime
representation is just a tuple ({:some, value} or {:none}), so it
costs nothing.
Summary
Functions
Returns the wrapped value if the argument is some(). If none(),
raises a RuntimeError with the message "Can't get value of None".
Returns the wrapped value if the argument is some(). If none(),
returns the second argument — either a value directly, or a 0-arity
function returning one.
Returns true if the argument is none(), false if some().
Returns true if the argument is some(), false if none().
Constructor macro: none() expands to {:none}. Works in both
expressions and patterns.
Converts an Option into a Result. some(val) becomes ok(val);
none() becomes error(...) carrying the second argument (or the
result of calling it, if a 0-arity function is supplied).
Returns the input as-is if it is some(). If none(), returns the
second argument — either an Option directly, or a 0-arity function
returning one.
Constructor macro: some(val) expands to {:some, val}. Works in both
expressions and patterns.
Lifts an arbitrary term into an Option: some(term) if non-nil,
none() otherwise.
Types
Functions
Returns the wrapped value if the argument is some(). If none(),
raises a RuntimeError with the message "Can't get value of None".
Examples
iex> some(5) |> get
5
Returns the wrapped value if the argument is some(). If none(),
returns the second argument — either a value directly, or a 0-arity
function returning one.
Examples
iex> some(5) |> get_or_else(2)
5
iex> none() |> get_or_else(2)
2
iex> none() |> get_or_else(fn -> 1 end)
1
Returns true if the argument is none(), false if some().
Examples
iex> is_none(none())
true
iex> is_none(some(5))
false
Returns true if the argument is some(), false if none().
Examples
iex> is_some(some(5))
true
iex> is_some(none())
false
Constructor macro: none() expands to {:none}. Works in both
expressions and patterns.
@spec ok_or_else(t(a), err | (-> err)) :: MonEx.Result.t(a, err) when a: any(), err: any()
Converts an Option into a Result. some(val) becomes ok(val);
none() becomes error(...) carrying the second argument (or the
result of calling it, if a 0-arity function is supplied).
Examples
iex> some(5) |> ok_or_else(2)
{:ok, 5} # same as ok(5)
iex> none() |> ok_or_else(:missing_value)
{:error, :missing_value} # same as error(:missing_value)
iex> none() |> ok_or_else(fn -> :oh_no end)
{:error, :oh_no}
Returns the input as-is if it is some(). If none(), returns the
second argument — either an Option directly, or a 0-arity function
returning one.
Examples
iex> some(5) |> or_else(some(2))
some(5)
iex> none() |> or_else(some(2))
some(2)
iex> none() |> or_else(fn -> some(1) end)
some(1)
Constructor macro: some(val) expands to {:some, val}. Works in both
expressions and patterns.
Lifts an arbitrary term into an Option: some(term) if non-nil,
none() otherwise.
Examples
iex> to_option(5)
some(5)
iex> to_option(nil)
none()