unwrap with fun or unwrap_or_else
Closed this issue · 2 comments
I was wondering if could be useful to modify the unwrap
function in order to accept a function as second param (matching).
The implementation:
@spec unwrap(any()) :: any()
def unwrap(a, b \\ nil)
def unwrap({:ok, a}, _), do: a
def unwrap({:error, error}, or_else) when is_function(or_else, 1), do: or_else.(error)
def unwrap(_, default), do: default
Otherwise what about something like the rust unwrap_or_else
function in Result?
In my mind could be very useful when you want to get the either :ok value or raise the :error. Or maybe when you need to transform the error into something else.
What do you think?
Thank you, Simone
Hi, sorry for the late reply!
what you're suggesting is remarkably similar to the either
function. The latter case could be handled by passing a function in the fail case such as fn _ -> default
.
What do you think? Maybe it's more ergonomic the way you've suggested, I wouldn't mind another opinion :)
Maronn, you are right. Second time fail :)