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?
Trending in Questions
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
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
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
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
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
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
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
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
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
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
ityonemo
Because you are compiling Foo after Bar.
NobbZ
Baris compiled, you see its output ofBar.__after_compile__Foois going to get compiled, it callsBar.__before_compile__, you see the outputBar.__before_compile__, because__MODULE__isBarin that context. You wan’t to checkenv.modto see the callers module.Foogets actuially compiled, its after compile gets called and you see itsFoo.__after_compile__.thojanssens1
Thank you! Can I ask something more as you seem to have good knowledge of macros?
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
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
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:
But again:
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 MyAppintoBar, which I thought means “wait forMyAppto be compiled before the compilation ofBar”, then I am sure thatBarwill not be compiled yet, but obviously got it all wrong again.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 MyAppadded.But I guess more important would be to know why you want to do that. Why should calling
use Bazregister a module attribute on a third module?thojanssens1
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_apptoTheLibrarysomehow. Often this is done withusebut 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__/1macro (because based on those options, I want to generate some functions into that module in the library).I could not find a way for
__before_compile__to get access to the option; keep having the blah-blah-blah-already-compiled.I do not know what is the best way for the user to pass the option to the library.
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
Again, you need to look into the callers environment. Macros get passed
__CALLER__as a magic variable, so you probably want toModule.register_attribute(__CALLER__.mod, :attr, [])or in this case even more appropriate, as you do not make it accumulating or persistent: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
compile-time output:
If it’s clearer, this is effectively equivalent: