Hi all
I want to pass a module to a macro and inside the macro, it should call a function from the passed module.
The following code snippet should clarify what I mean.
This module I will pass to macro
defmodule Plug do
def init do
5
end
end
and the macro
defmodule Macros do
defmacro cosume(module) do
var = module.init
quote do
unquote(var)
end
end
end
I compiled everything and tried to use the macro:
iex(1)> import_file("macros.exs")
{:module, Macros,
<<70, 79, 82, 49, 0, 0, 5, 208, 66, 69, 65, 77, 69, 120, 68, 99, 0, 0, 0, 158,
131, 104, 2, 100, 0, 14, 101, 108, 105, 120, 105, 114, 95, 100, 111, 99, 115,
95, 118, 49, 108, 0, 0, 0, 4, 104, 2, ...>>, {:consume, 1}}
iex(2)> import_file("plug.exs")
{:module, Plug,
<<70, 79, 82, 49, 0, 0, 4, 140, 66, 69, 65, 77, 69, 120, 68, 99, 0, 0, 0, 127,
131, 104, 2, 100, 0, 14, 101, 108, 105, 120, 105, 114, 95, 100, 111, 99, 115,
95, 118, 49, 108, 0, 0, 0, 4, 104, 2, ...>>, {:init, 0}}
iex(3)> import Plug
Plug
iex(4)> require Macros
Macros
iex(5)> Macros.consume(Plug)
** (UndefinedFunctionError) function :__aliases__.init/1 is undefined (module :__aliases__ is not available)
:__aliases__.init({:__aliases__, [counter: 0, line: 5], [:Plug]})
expanding macro: Macros.consume/1
iex:5: (file)
iex(5)>
What am I doing wrong?
Thanks