OkThen.Result.none_then
none_then
, go back to OkThen.Result module for more information.
none_then(result, func_or_value)
Specs
none_then(result_input(), func_or_value(out)) :: out when out: any()
If result
is tagged :none
, calls func_or_value
and returns the result. If func_or_value
is not a function, then it is returned as-is.
If result
is not tagged with the specified tag
atom, result
is returned as-is.
Use this function to pipe results into functions that return tagged tuples.
Be aware that no attempt is made to ensure the return value from the function is a tagged tuple.
However, all functions are tolerant of untagged results, and on input will interpret them as an
{:untagged, value}
tuple.
Equivalent to tagged_then(result, :none, func_or_value)
. See tagged_then/3
.
A note about unwrapped nils
If result
is nil
, it is intprereted as :none
. This may be slightly unintuitive, so if
you're curious, this is the reason:
Untagged results are internally wrapped as an {:untagged, any()}
using Result.from_as(value, :untagged)
, and if value
is nil
, the return value of Result.from_as/2
will always be
:none
.
Examples
iex> :none |> Result.none_then(:ok)
:ok
iex> :none |> Result.none_then(fn {} -> {:ok, "hello"} end)
{:ok, "hello"}
iex> :error |> Result.none_then(fn -> {:ok, "hello"} end)
:error
iex> :error |> Result.none_then(fn _ -> {:ok, "hello"} end)
:error
iex> {:error, 1} |> Result.none_then(fn _ -> {:ok, "hello"} end)
{:error, 1}
iex> {:error, 1, 2} |> Result.none_then(fn _ -> {:ok, "hello"} end)
{:error, 1, 2}
iex> :something_else |> Result.none_then(fn _ -> {:ok, "hello"} end)
:something_else
iex> "bare value" |> Result.none_then(:ok)
"bare value"
iex> "bare value" |> Result.none_then(fn _ -> :ok end)
"bare value"
iex> nil |> Result.none_then(:ok)
:ok