Macros, like functions, have to be compiled and available before they can be called. Since macros are expanded at compile time, they need to be defined in a separate module and can’t be defined in the same module that calls them. Which is why your second example works. Its actually the same for functions. For example the following would also not work.
defmodule MyMod do
# won't be available to be called during module definition
def my_fun do
[1,2, 3]
end
# my_fun/0 isn't compiled and available here
# but this would work if `my_fun/0` was defined in another module
for i <- my_fun() do
def other_fun(unquote(i)), do: i
end
end
Hi kip; the fact is that the definition of the macro is in module Problem, and used in module Injected. The same behavior occurs it you split in two files each for a module. Also, the second version posted works, with no problem. I’ve reported this to elixir-lang, may be a bug (https://github.com/elixir-lang/elixir/issues/10308)
Thanks for your reply