With statement with arrow or match operator. Wha'ts the difference?

I’ve been working wit Elixir for a few weeks. Quick question:

In the next code, What’s the difference between the first two
left = right
and the last
left <- right

I only’ve used with with the <- before.

with {:ok, item} = Item.createl(%{ name: "Sample Item", item_id: id }),
       {:ok, option} = Option.create_option(%{ title: "Sample Choice",  item_id: item.id }),
           option <- Repo.preload(option, :item)
1 Like

From the documentation:

The behaviour of any expression in a clause is the same as outside. For example, = will raise a MatchError instead of returning the non-matched value:

with :foo = :bar, do: :ok
#=> ** (MatchError) no match of right hand side value: :bar

An example that is a bit more obvious:

iex(1)> with :a = :b do :ok else _ -> :error end
warning: "else" clauses will never match because all patterns in "with" will always match

** (MatchError) no match of right hand side value: :b
    (stdlib) erl_eval.erl:453: :erl_eval.expr/5
    (stdlib) erl_eval.erl:126: :erl_eval.exprs/5
    (iex) lib/iex/evaluator.ex:257: IEx.Evaluator.handle_eval/5
    (iex) lib/iex/evaluator.ex:237: IEx.Evaluator.do_eval/3
    (iex) lib/iex/evaluator.ex:215: IEx.Evaluator.eval/3
    (iex) lib/iex/evaluator.ex:103: IEx.Evaluator.loop/1
iex(1)> with :a <- :b do :ok else _ -> :error end

I see that now. Thanks a lot.