4 ms·
Here is a better version that checks if expr1 is a variable. The condition "is_atom(x)" might be too strong. defmodule OurMacro do defmacro cond_assign(e
by lkuty 3y ago
Here is a better version that checks if expr1 is a variable. The condition "is_atom(x)" might be too strong.
defmodule OurMacro do
defmacro cond_assign(expr1, expr2, do: block) do
case expr1 do
{x, _, y} when is_atom(x) and is_atom(y) -> nil
_ -> :erlang.error(RuntimeError.exception("The lvalue is not a variable: #{Macro.to_string(expr1)}"))
end
quote do
unquote(expr1) = if unquote(expr2), do: unquote(block), else: unquote(expr1)
end
end
end