I had a problem where I needed to make sure a string was returned from a function that was part of a with clause. I thought, “You can’t pattern match here, right? That would just be too convenient.”
Turns out you totally can. Here’s an example from the docs.
users = %{"melany" => "guest", "bob" => :admin}
with {:ok, role} when not is_binary(role) <- Map.fetch(users, "bob") do
{:ok, to_string(role)}
end
Top comments (1)
This is why the
with
syntax is so awesome.If your pattern match fails, it tries to pattern match in the
else
block too, which allows you to handle all of your errors below, and all of your happy paths above, so at a glance it's clear what your intent is.This is also basis to another really interesting pattern that I've really come to like - tagged tuple.
Sometimes your pattern matching might return something pretty generic, so it's hard for you to handle the errors for them, for example:
Tagged tuple changes this, by wrapping each execution in a tuple, like this:
I have an example running in the wild here:
github.com/edisonywh/committee/blo...
You can see how elegant error handling gets!