Getting a list of modules within a macro -- returns :undefined

Similar to Question on :application.get_key(:my_app, :module)

Why will the 1st call return :undefined, whereas the 2nd will do a list of modules?

defmodule App123.ModLoad do
  defmacro __using__(opts) do
    quote bind_quoted: [opts: opts] do

      var1 = :application.get_key(:app123)
      IO.puts("****:application.get_key: #{Kernel.inspect(var1)}")
      # :undefined
      # why?

      var2 = :code.all_loaded()
      IO.puts("****:code.all_loaded: #{Kernel.inspect(var2)}")
      # a list of modules
      # ...
      # ............

What I need is a list of modules obtained within a macro, and which I’ll then work on with:

  var3 = :application.get_key(:app123, :modules)
  # some work with var3
  # filter out some of them
  # then --> generate a module attribute

However, even var1 is :undefined

Why? And how to fix this?

Namely, the desired result will be that I’ll call use App123.ModLoad in other module(s) – App123.Module2 – which then will generate a certain module attribute – @my_modules – with a list of certain modules I need. At the compile time..

defmodule App123.Module2 do
    use App123.ModLoad

    def func1 do
      # now I'll have access to @my_modules 
      # which contains the modules I need
1 Like

Sorry that nobody responded yet.

As a general advice, try and make a small public project on GitHub that demonstrates the issue. In my observations people will be more willing to engage then.

1 Like