OkThen.Result.default_as
You're seeing just the function
default_as
, go back to OkThen.Result module for more information.
Link to this function
default_as(result, tag, func_or_value)
Specs
default_as(input, tag, (() -> out) | out) :: input | tag | {tag, out} when input: result_input(), tag: atom(), out: any()
If result
is tagged :none
, returns func_or_value
wrapped as a result with the given tag
.
Otherwise, returns result
. If func_or_value
is a function, the returned value is used as the
new value.
If the new value is nil
, then the result will remain :none
. Consider using none_then/2
if
you don't want this behaviour.
If result
is not tagged :none
, result
is returned as-is.
Examples
iex> :none |> Result.default_as(:ok, "hello")
{:ok, "hello"}
iex> :none |> Result.default_as(:error, {})
:error
iex> :none |> Result.default_as(:something, nil)
:none
iex> :none |> Result.default_as(:ok, fn -> 1 end)
{:ok, 1}
iex> :none |> Result.default_as(:ok, fn {} -> 1 end)
{:ok, 1}
iex> {:none, 1} |> Result.default_as(:ok, & &1)
{:ok, 1}
iex> :ok |> Result.default_as(:ok, "hello")
:ok
iex> {:ok, 1} |> Result.default_as(:ok, "hello")
{:ok, 1}
iex> {:ok, 1, 2} |> Result.default_as(:ok, "hello")
{:ok, 1, 2}
iex> {:anything, 1} |> Result.default_as(:ok, "hello")
{:anything, 1}
iex> "bare value" |> Result.default_as(:ok, "hello")
"bare value"