Solution v1.0.0 released! - A macro-based solution to working with ok/error tuples in `case` and `with` statements and in collections.
Solution is a library to help you with working with ok/error-tuples in case
and with
-expressions by exposing special matching macros, as well as some extra helper functions to work with them in collections.
An example:
x = {:ok, 10}
y = {:ok, 42, "Oh, here's another argument!"}
z = {:ok, "something we do not care about"}
swith ok(res) <- x,
ok(res2) <- y,
ok() <- z do
"We have: \#{res} \#{res2}"
else
other -> "Failure at #{inspect(other)}"
end
# => "We have: 10 33"
Note how it did not matter that y
contains more than one value in the tuple, or that z
does not return a plain :ok
. But if they would have, the outcome of the code would have been exactly the same.
Solution handles these cases for you exactly to allow you to have less of a tight coupling between the places where an ok/error tuple is returned and where it is used:
-
If at some point a caller decides to change from
:ok
to{:ok, some_value}
, then that will not break your code. -
If at some point a caller decides to change from
{:ok, some_value}
to{:ok, some_value, some_metadata}
, then that will not break your code.
Besides allowing tricks like above, Solution also contains a small module containing some helper functions to combine collections of ok/error tuples in common ways.
Find it at: HexPM, GitHub, ElixirForum.