One line `with` with else

Hi folks!

I am using Elixir 1.3.3.

One line if works just fine:

iex(3)> if true, do: :ok, else: :error
:ok
iex(4)> if false, do: :ok, else: :error
:error

One line with works fine without else:

iex(5)> with true <- true, do: :ok
:ok

One line with with else does not work:

iex(6)> with true <- true, do: :ok, else: {:error, _msg} -> :error
** (SyntaxError) iex:6: syntax error before: '->'

Maybe someone knows the reason.
Thanks!

3 Likes

When using the one-liner syntax with ->, you need to wrap the whole left -> right expression in parens otherwise it is unable to know where it starts and where it finishes:

iex(1)>  with true <- true, do: :ok, else: ({:error, _msg} -> :error)
:ok
7 Likes

Great! Thank you very much, José!