“application.get_key > modules” 将返回:未定义

huangapple go评论48阅读模式
英文:

"application.get_key > modules" will return :undefined

问题

I want to get a list of the modules in a certain namespace at the compile time, as a macro.

defmodule MyApp.MyModuleLoader do
  defmacro __using__(opts) do
    quote bind_quoted: [opts: opts] do

      a1 = :application.get_key(:my_app)
      modules = :application.get_key(:my_app, :modules)

      IO.puts("****:a1: #{Kernel.inspect(a1)}")
      IO.puts("****:modules: #{Kernel.inspect(modules)}")

      # ===> :undefined

      # filter out the ones I don't need...
      # ....
    end
  end
end

I'll then use MyModuleLoader in a schema-model module. But it'll print out :undefined for both variables during compilation.

Why? How to fix this?

英文:

I want to get a list of the modules in a certain namespace at the compile time, as a macro.

defmodule MyApp.MyModuleLoader do
  defmacro __using__(opts) do
    quote bind_quoted: [opts: opts] do

      a1 = :application.get_key(:my_app)
      modules = :application.get_key(:my_app, :modules)

      IO.puts("****:a1: #{Kernel.inspect(a1)}")
      IO.puts("****:modules: #{Kernel.inspect(modules)}")

      # ====> :undefined

      # filter out the ones I don't need...
      # ....
    end
  end
end

I'll then use MyModuleLoader in a schema-model module.
But it'll print out :undefined for both variables during compilation.

Why? How to fix this?

答案1

得分: 1

If we add Application.ensure_loaded(:my_app) like:

defmodule MyApp.MyModuleLoader do
  defmacro __using__(opts) do
    quote bind_quoted: [opts: opts] do
      Application.ensure_loaded(:my_app) # <-
      
      a1 = :application.get_key(:my_app)
      modules = :application.get_key(:my_app, :modules)

      IO.puts("****:a1: #{Kernel.inspect(a1)}")
      IO.puts("****:modules: #{Kernel.inspect(modules)}")

      # ===> :undefined

      # filter out the ones I don't need...
      # ....
    end
  end
end

Then the output (ran via iex -S mix) will be:

****:a1: :undefined
****:modules: {:ok, [<list of modules>]}

After changing IO.puts(...) to IO.inspect(...) we get the same output from mix compile command.

英文:

If we add Application.ensure_loaded(:my_app) like:

defmodule MyApp.MyModuleLoader do
  defmacro __using__(opts) do
    quote bind_quoted: [opts: opts] do
      Application.ensure_loaded(:my_app) # &lt;-

      a1 = :application.get_key(:my_app)
      modules = :application.get_key(:my_app, :modules)

      IO.puts(&quot;****:a1: #{Kernel.inspect(a1)}&quot;)
      IO.puts(&quot;****:modules: #{Kernel.inspect(modules)}&quot;)

      # ====&gt; :undefined

      # filter out the ones I don&#39;t need...
      # ....
    end
  end
end

Then the output (ran via iex -S mix) will be:

****:a1: :undefined
****:modules: {:ok, [&lt;list of modules&gt;]}

After changing IO.puts(...) to IO.inspect(...) we get the same output from mix compile command.

huangapple
  • 本文由 发表于 2023年5月7日 11:08:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76192031.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定