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 mix compile — Mix v1.20.2

  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

Tee
can someone please explain to me how Enum.reduce works with maps
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
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
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
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
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29603 241
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
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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement