Macros using the <- operator

We currently use Elixir 1.7 and have the follow macro defined to simplify logic in with-do blocks:

defmacro ensure(condition, [fail: error]) do
    quote do
      :ok <- if unquote(condition), do: :ok, else: unquote(error)

Usage is something like this:

with ensure(check_condition?(x), fail: {:error, :bad_condition}),
do: :ok

In Elixir 1.8 and above, this macro no longer compiles. I get an error undefined function <-/2. Is there any way to define this macro such that it works with the current compiler? Or something similar I could do instead? We really like this syntactic sugar for with-do blocks.

1 Like