nmichel

nmichel

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 -

Showing Posts 1 to 1

nmichel

nmichel OP

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:

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
widianto
I think I’ve found a small improvement I could contribute to <%= web_namespace %>.CoreComponents (installer/templates/phx_web/compo...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
aseigo
ICal is a library for interacting with iCalendar data. It parses iCalendars into typed Elixir structs via ICal.from_ics, and can prepare ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews