thojanssens1

thojanssens1

Why is __after_compile__/2 called before __before_compile__/1?

defmodule Bar do
  @after_compile __MODULE__

  defmacro __before_compile__(_env) do
    IO.inspect("#{__MODULE__}.__before_compile__")
  end

  def __after_compile__(_env, _bytecode) do
    IO.inspect("#{__MODULE__}.__after_compile__")
  end
end

defmodule Foo do
  require Bar

  @before_compile Bar
  @after_compile __MODULE__

  def __after_compile__(_env, _bytecode) do
    IO.inspect("#{__MODULE__}.__after_compile__")
  end
end

Will print:

“Elixir.Bar.after_compile
“Elixir.Bar.before_compile
“Elixir.Foo.after_compile
[Bar, Foo]

Why is after_compile called before before_compile for the Bar module?

Marked As Solved

NobbZ

NobbZ

  1. Bar is compiled, you see its output of Bar.__after_compile__
  2. Foo is going to get compiled, it calls Bar.__before_compile__, you see the output Bar.__before_compile__, because __MODULE__ is Bar in that context. You wan’t to check env.mod to see the callers module.
  3. Foo gets actuially compiled, its after compile gets called and you see its Foo.__after_compile__.

Also Liked

ityonemo

ityonemo

Because you are compiling Foo after Bar.

LostKobrakai

LostKobrakai

Baz is already compiled when used in MyApp. MyApp is not yet compiled. In macros it’s important to be aware if you‘re working in the context of the macro (__MODULE__ is the module with the macro definition) or if you‘re working on the AST of the macros result, which is effectively becoming part of the module calling the macro (__MODULE__ refers to the module calling the macro).

NobbZ

NobbZ

Again, you need to look into the callers environment. Macros get passed __CALLER__ as a magic variable, so you probably want to Module.register_attribute(__CALLER__.mod, :attr, []) or in this case even more appropriate, as you do not make it accumulating or persistent:

defmacro __using__(_opts) do
  quote do
    @attr []
  end
end

Or instead of the empty list use any other default value you prefer.

Beeing aware of the current context you are in is important for macros.

Perhaps give Metaprogramming Elixir by @chrismccord a read? I only skimmed about the first quarter of it, but it helped me to understand macros enough for my usecase.

Where Next?

Popular in Questions 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
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
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
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
yawaramin
In the Dialyzer docs ( dialyzer — OTP 29.0.2 (dialyzer 6.0.1) ), there is a way to turn off a specific warning for a function: -dialyzer...
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

skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
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
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
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
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
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 47930 226
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

We're in Beta

About us Mission Statement