Astarno

Astarno

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!

First 2 of 2 Posts Switch mode

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:

Astarno

Astarno OP

That is unfortunate, at least in my case. Thanks for your reply!

— 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
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
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
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New

We're in Beta

About us Mission Statement