You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Another example is Plug itself. Plug will only call the next plug if the :halted field in the connection is false. Therefore a whole plug pipeline can be written as:
If any of them does not match, because :halted is true, the pipeline is aborted.
Similarly to for, variables bound inside with won't leak. Also similar to for, with allows "bare expressions". For example, imagine you need to calculate a value before calling the next match, you may write:
with
isfor
's younger brother. Imagine we have two functions:While
for
is used to match on values out of a collection:with
is used to match on values directly:Because all values matched, the
do
block was executed, returning its result. If a value does not match, it will abort the chain:Since
error(2)
did not match{:ok, y}
, thewith
chain aborted, returning{:error, 2}
.There are many different scenarios on every day Elixir code that we can use
with
. For example, it is useful to avoid nesting "case"s:Can now be rewritten as:
Another example is Plug itself. Plug will only call the next plug if the
:halted
field in the connection is false. Therefore a whole plug pipeline can be written as:If any of them does not match, because
:halted
is true, the pipeline is aborted.Similarly to
for
, variables bound insidewith
won't leak. Also similar tofor
,with
allows "bare expressions". For example, imagine you need to calculate a value before calling the next match, you may write:The text was updated successfully, but these errors were encountered: