thojanssens1

thojanssens1

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?

Showing Posts 1 to 10

ityonemo

ityonemo

Because you are compiling Foo after Bar.

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__.
thojanssens1

thojanssens1 OP

Thank you! Can I ask something more as you seem to have good knowledge of macros?

defmodule Baz do
  defmacro __using__(_opts), do: Module.register_attribute(__MODULE__, :attr, [])
end

defmodule MyApp do
  use Baz
end

== Compilation error in file test.exs ==
** (ArgumentError) could not call Module.register_attribute/3 because the module Baz is already compiled

Why does it say Baz is already compiled, when I’m executing __using__? As to my understanding, __using__ is run on compile time, so the file should not be compiled yet?

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).

thojanssens1

thojanssens1 OP

So if I understand right, if a module A calls __using__ of a module B, then __using__ macro of B will be executed after B is compiled.

Then if I try to register an attribute on another module:

defmodule Bar do
end

defmodule Baz do
  defmacro __using__(_), do: Module.register_attribute(Bar, :attr, [])
end

defmodule MyApp do
  use Baz
end

But again:

** (ArgumentError) could not call Module.register_attribute/3 because the module Bar is already compiled

So Bar is already compiled, but how can I tell that it should not compile before registering the attributes in the __using__ macro of Baz?

I tried require MyApp into Bar, which I thought means “wait for MyApp to be compiled before the compilation of Bar”, then I am sure that Bar will not be compiled yet, but obviously got it all wrong again.

LostKobrakai

LostKobrakai

In your example there’s no compile time relationship between MyApp(/Baz) and Bar, so they’re compiled in parallel. I’m not sure though why exactly it doesn’t work with require MyApp added.

But I guess more important would be to know why you want to do that. Why should calling use Baz register a module attribute on a third module?

thojanssens1

thojanssens1 OP

These were indeed all examples with Foo/Bar/Baz placeholders because I did not want to bloat the problem with application-specific details. But here is what I try to do:

I want my library to receive options at compile time, e.g. the user can pass option otp_app: :my_app to TheLibrary somehow. Often this is done with use but I don’t need to inject anything in a user’s module.

Then in some module of the library, I need to use those options in a __before_compile__/1 macro (because based on those options, I want to generate some functions into that module in the library).

  1. I could not find a way for __before_compile__ to get access to the option; keep having the blah-blah-blah-already-compiled.

  2. I do not know what is the best way for the user to pass the option to the library.

LostKobrakai

LostKobrakai

As the option is already compile time only you can just as easily use the app env for configuration and the value will be available everywhere without intruducing compile time dependencies just to pass along configuration values.

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.

ityonemo

ityonemo

defmodule A do
  defmacro __using__(opts) do
    otp_app = opts[:otp_app]      # this gets set to :foo
    quote do                           # note 'use' is called by B, so the next two lines live in B.
      @otp_app unquote(otp_app)  # becomes @otp_app :foo
      @before_compile A 
    end
  end

  defmacro __before_compile__(_) do
    __CALLER__.module
    |> IO.inspect(label: "module")
    |> Module.get_attribute(:otp_app)
    |> IO.inspect(label: "otp app")
    quote do end
  end
end

defmodule B do
  use A, otp_app: :foo
end

compile-time output:

module: B
otp app: :foo

If it’s clearer, this is effectively equivalent:

defmodule A do
  defmacro __using__(opts) do
    otp_app = opts[:otp_app]
    quote do
      @otp_app unquote(otp_app)
    end
  end

  defmacro __before_compile__(_) do
    __CALLER__.module
    |> IO.inspect(label: "module")
    |> Module.get_attribute(:otp_app)
    |> IO.inspect(label: "otp app")
    quote do end
  end
end

defmodule B do
  use A, otp_app: :foo
  @before_compile A
end
— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews