The with Expression

Understand the usage of the with keyword in Elixir.

We'll cover the following...

The with expression serves double duty.

  • First, it allows us to define a local scope for variables. If we need a couple of temporary variables when calculating something, and we don’t want those variables to leak out into the wider scope, we use with.
  • Second, it gives us some control over pattern-matching failures.

The following code finds the values for the _lp user.

content = "Now is the time"

lp = with {:ok, file} =
...