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

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

Other popular topics Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
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 55125 245
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

We're in Beta

About us Mission Statement