Astarno

Astarno

Manipulate the AST before compilation

Hi there,

Is there any way to change/edit the code/ast of a module before it compiles? I am trying to use the Interceptor library to add logging functionality to a module. In order to work, this library requires all functions to be traced to either be wrapped in a special do block or to be annotated.

Now, I would like for the user to be able to just write his regular code without having to worry about this wrapping/annotation at all. As such, my question is if it would be possible to leverage some metaprogramming, module callbacks or something else to automatically make the necessary changes to the code at compile time.

For example, let’s say I want to trace all functions, could I make something such that the code below:

defmodule Stack do
  use GenServer
  require Interceptor, as: I

      def start_link(default) when is_list(default) do
        GenServer.start_link(__MODULE__, default)
      end
  
      @impl true
      def init(stack) do
        {:ok, stack}
      end
  
      @impl true
      def handle_call(:pop, _from, [head | tail]) do
        {:reply, head, tail}
      end
  
      @impl true
      def handle_cast({:push, element}, state) do
        {:noreply, [element | state]}
      end

end

Either gets manipulated to look like this:

defmodule Stack do
  use GenServer
  require Interceptor, as: I

    I.Intercept do 
       def start_link(default) when is_list(default) do
         GenServer.start_link(__MODULE__, default)
       end

       @impl true
       def init(stack) do
         {:ok, stack}
       end

       @impl true
       def handle_call(:pop, _from, [head | tail]) do
         {:reply, head, tail}
       end

       @impl true
       def handle_cast({:push, element}, state) do
         {:noreply, [element | state]}
       end
    end
end

Or gets manipulated to look like this:

defmodule Stack do
  use GenServer
  require Interceptor, as: I

      @intercept true
      def start_link(default) when is_list(default) do
        GenServer.start_link(__MODULE__, default)
      end
  
      @impl true
      @intercept true
      def init(stack) do
        {:ok, stack}
      end
  
      @impl true
      @intercept true
      def handle_call(:pop, _from, [head | tail]) do
        {:reply, head, tail}
      end
  
      @impl true
      @intercept true
      def handle_cast({:push, element}, state) do
        {:noreply, [element | state]}
      end

end

Any help would be greatly appreciated!

Marked As Solved

josevalim

josevalim

Creator of Elixir

By design we don’t allow so. Any transformation in the code has to be explicit. So you have to wrap it with the block yourself. :slight_smile:

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
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
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
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
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

Other popular topics Top

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
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30877 112
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 53690 245
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New

We're in Beta

About us Mission Statement