OkThen.Result.default
You're seeing just the function
default
, go back to OkThen.Result module for more information.
Link to this function
default(result, func_or_value)
Specs
default(input, (() -> out) | out) :: input | :ok | ok(out) when input: result_input(), out: any()
If result
is tagged :none
, returns func_or_value
wrapped as an :ok
result. 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.
Equivalent to default_as(result, :ok, func_or_value)
. See default_as/3
.
Examples
iex> :none |> Result.default("hello")
{:ok, "hello"}
iex> :none |> Result.default({})
:ok
iex> :none |> Result.default(nil)
:none
iex> :none |> Result.default(fn -> 1 end)
{:ok, 1}
iex> :none |> Result.default(fn {} -> 1 end)
{:ok, 1}
iex> {:none, 1} |> Result.default(& &1)
{:ok, 1}
iex> :ok |> Result.default("hello")
:ok
iex> {:ok, 1} |> Result.default("hello")
{:ok, 1}
iex> {:ok, 1, 2} |> Result.default("hello")
{:ok, 1, 2}
iex> {:anything, 1} |> Result.default("hello")
{:anything, 1}
iex> "bare value" |> Result.default("hello")
"bare value"