Hi
There is something I don’t get about macros, modules and aliasing.
Consider the following code snippets.
Module Foo
exports macro show/1
that just log the received AST fragment on standard output.
defmodule Foo do
defmacro show(frag) do
IO.puts "#{inspect frag}"
frag
end
end
Then consider the following two useless modules Outer
and Outer.Inner
.
defmodule Outer do
defmodule Inner do
def foo do
:foo
end
end
end
Now lets play with aliasing.
defmodule Test do
import Foo
alias Outer, as: A
alias Outer.Inner, as: B
alias A.Inner, as: C
show A
show B
show C
show C.foo()
ast = quote do: A
IO.puts "ast #{inspect ast}"
end
Here is the console log when I compile the code.
iex(1)> c("./test/demo.ex")
{:__aliases__, [counter: 0, line: 23], [:A]} # No aliasing info
{:__aliases__, [counter: 0, line: 24], [:B]}
{:__aliases__, [counter: 0, line: 25], [:C]}
{{:., [line: 26], [{:__aliases__, [counter: 0, line: 26], [:C]}, :foo]}, [line: 26], []}
ast {:__aliases__, [alias: Outer], [:A]} # quote => Aliasing info !
[Test, Outer, Outer.Inner, Foo]
iex(2)>
What puzzles me, is that in show/1
macro calls, module fragments do not hold any aliasing information, while when quoted, it does.
Why don’t I have aliasing information during macro expansion ?
Many thanks !
Nicolas -