nmichel

nmichel

Module aliasing in macros

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 -

Most Liked

nmichel

nmichel

Hi,

I pursued my investigations further, and modified the Foo.show/1 function as follow.

defmodule Foo do
    defmacro show(frag = {:__aliases__, _, refs}) do
        resolved = Macro.expand(frag, __CALLER__)
        IO.puts """
        Alias
        frag \t #{inspect frag}
        resolved #{inspect resolved}
        """
        frag
    end

    defmacro show(frag = {{:., _, [path, target]}, _, _}) do
        resolved = Macro.expand(path, __CALLER__)
        IO.puts """
        Call
        path \t #{inspect path}
        resolved #{inspect resolved}#{inspect target}
        """
        frag
    end
end

Basically I catch fragments bearing aliasing information to make some experiments.

Here is the output

Alias
frag     {:__aliases__, [counter: 0, line: 38], [:A]}
resolved Outer

Alias
frag     {:__aliases__, [counter: 0, line: 39], [:B]}
resolved Outer.Inner

Alias
frag     {:__aliases__, [counter: 0, line: 40], [:C]}
resolved Outer.Inner

Call
path     {:__aliases__, [counter: 0, line: 41], [:C]}
resolved Outer.Inner:foo

ast {:__aliases__, [alias: Outer], [:A]}

We can see that, though fragments do not bear aliasing information, Macro.expand resolves aliases at compile time.

Cheers

Nicolas -

P.S. The question may rise of knowing WHY I needed to have those resolved symbols. Well, I wanted to be able to take some decisions based on the knowledge that a function is exported by a certain module … it proved to be a wrong way, but the underlying purely technical is still interesting :slight_smile:

Where Next?

Popular in Questions Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54996 245
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

We're in Beta

About us Mission Statement