View Source Dreamy.Option (dreamy v1.0.0)
Functions for use with Options
Summary
Functions
Build an Option from a Result
Get the value of an option
Get the value of an option, throwing an error if empty
Returns an empty Option
builds an Option from a value
Build an Result from an Option
Types
@type empty() :: {Dreamy.Option, :empty}
@type t(v) :: {Dreamy.Option, v} | empty()
Functions
@spec from_result(Dreamy.Result.t(ok, term())) :: t(ok) when ok: var
Build an Option from a Result
Examples
iex> use Dreamy
...> from_result({:error, "err"})
{Dreamy.Option, :empty}
iex> use Dreamy
...> from_result({:ok, "OK"})
{Dreamy.Option, "OK"}
@spec get(t(v)) :: {:ok, v} | :error when v: var
Get the value of an option
Examples
iex> use Dreamy
...> empty()
...> |> get()
:error
iex> use Dreamy
...> option("Hello World")
...> |> get()
{:ok, "Hello World"}
@spec get!(t(v)) :: v when v: var
Get the value of an option, throwing an error if empty
Examples
iex> use Dreamy
...> empty()
...> |> get!()
** (RuntimeError) Empty Option
iex> use Dreamy
...> option("Hello World")
...> |> get!()
"Hello World"
Returns an empty Option
Examples
iex> use Dreamy
...> empty()
{Dreamy.Option, :empty}
@spec option(Dreamy.Types.nullable(v)) :: t(v) when v: var
builds an Option from a value
Examples
iex> use Dreamy
...> option(nil)
{Dreamy.Option, :empty}
iex> use Dreamy
...> empty() == option(nil)
true
iex> use Dreamy
...> option("Hello World")
{Dreamy.Option, "Hello World"}
@spec to_result(t(v)) :: {:ok, v} | {:error, nil} when v: var
Build an Result from an Option
Examples
iex> use Dreamy
...> empty()
...> |> to_result()
{:error, nil}
iex> use Dreamy
...> option("OK")
...> |> to_result()
{:ok, "OK"}