thojanssens1

thojanssens1

Generate a list of functions at compile-time into a module of the library

For a library, we get the list of all Ecto Schema modules of the user’s app, and want to generate functions (through macros) into a module of our library.

I can retrieve the list of Ecto schema already:

{:ok, modules} = :application.get_key(APP_NAME, :modules)

modules
|> Enum.filter(&({:__schema__, 1} in &1.__info__(:functions)))
|> IO.inspect()

Can I call :application.get_key(APP_NAME, :modules) at compile-time?

If so, I still encounter two problems:

  1. there is no concept of a “main” application in the Elixir/Erlang world, so how should I get the application(s) of the user (see APP_NAME above)? Or maybe I have to rethink and do it in another way, but kind of lost (maybe solution in point 2 below).

  2. how do I run these macros at compile-time? Ideally the user doesn’t need to write any code. Or maybe the library’s user can write a line of code at startup with the list of his applications (which would solve point 1.)? LibraryName.init([:the_app]), but it should happen at compile-time for the macros…

Marked As Solved

kip

kip

ex_cldr Core Team

As you point out, introspecting a project to “find all the modules that have some behaviour” is a challenge because a project can have many applications and there isn’t a digraph except through analysing the dependency tree.

Secondly, having been down a similar path, this kind of “magically find all the modules” tends to end badly. Part of it is the desire in Elixir to be explicit which helps remove magic. Part of it is that it ties too much to internal implementation which can and does change.

That said, there are some approaches you might consider:

  1. Mix lets you specify a compiler chain. Since you want to generate a module based upon previously compiled modules, you could create your own “compiler” which is just a function that gets called after everything else is compiled. See https://hexdocs.pm/mix/Mix.Tasks.Compile.html

  2. You can traverse dependencies by having fun with Mix.Dep.cached/0 and then doing :application.get_key(APP_NAME, :modules) or similar. You would do this in your “compiler” which would be configured to be the last compiler to run - thereby guaranteeing all the dips and app code are compiled first and therefore can be introspected.

All in all though, having travelled this road a few times, I would recommend make your intention explicit not implicit.

Where Next?

Popular in Questions Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
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
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
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

We're in Beta

About us Mission Statement