Using the with form

Learn to use Elixir's with to combine multiple matching clauses.

Introduction to Elixir’s with

Elixir’s special form with allows us to combine multiple matching clauses. If all clauses match, the code executes and returns the do block result. If one clause doesn’t match, the code stops and returns the value of the non-matching clause. It’s useful to combine clauses that can result in unexpected values. Then we can handle the error at a convenient point, reducing the conditional code for each error. We use with when we have function pipelines that can result in an error.

Get hands-on with 1200+ tech skills courses.