Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check pattern whether pattern match has succeeded or failed

In Elixir, is there an idiomatic way to check that a match has succeeded or failed without using error mechanisms like try/rescue?

like image 694
category Avatar asked Dec 14 '22 20:12

category


1 Answers

While the answer by @PatNowak is absolutely correct, I personally prefer to have a private function handler(s) with different clauses to make the code more structured:

Insertion:

changeset
|> Repo.insert()
|> handle_insert()

Handlers:

defp handle_insert({:ok, user}), do: happy_path
defp handle_insert({:error, changeset}), do: sad_path
defp handle_insert(whatever), do: Logger.debug()
like image 95
Aleksei Matiushkin Avatar answered May 23 '23 05:05

Aleksei Matiushkin