Elixir macros and bind_quoted

前端 未结 2 699
悲哀的现实
悲哀的现实 2021-02-08 17:49

I have a macro that defines a module like so.

defmodule Bar do
  def bar do
    IO.puts \"I am #{inspect __MODULE__}\"
  end
end

defmodule MacroFun do

  defmac         


        
2条回答
  •  故里飘歌
    2021-02-08 18:42

    I think that's because the place where you are unquoting (binding) the variable name.

    In the first case, you are unquoting the variable name when creating a module, thus binding the variable at that moment would require to check for context (check if the code is inside another module, for example). So, you get your current atom plus the appropriate context: Runner.Foo.

    In the second case, you are unquoting the variable name before it's placed in a context, therefore its value will not change and it'll be the atom Foo (no context).

提交回复
热议问题