Hello everyone. I have an issue with macros that I don’t know how to solve.
I have a macro:
defmacro command(expr, do: block) do
quote do
def handle_message(unquote(expr), msg) do
Logger.info("[CONSUMER][EVENT] Recieved command #{unquote(expr)}")
unquote(block)
end
end
end
And I intended using it like this
command "foo" do
IO.inspect(msg)
end
But I get and error
** (CompileError) lib/myapp.ex:27: undefined function msg/0
The issue is understandable, it seems that it is trying to access msg
before macro is expanded.
But how is that possible? Aren’t macros getting expanded on compile time?
Is what I am trying to achieve illegal or am I missing something?
Thanks for your help!