2016-05-15 56 views
1

我试图用下面的代码来定义两个宏,但是它失败了** (CompileError) iex:12: undefined function name/0。 函数参数name不能在defmacro的do块中不加引号。Elixir:如何使用List作为变量定义多个宏?

这是什么原因? 有什么办法可以解决这个问题吗?

(花好月圆版本1.2.5)

defmodule IEx.MyHelpers do 

    def default_env do 
    __ENV__ 
    end 

    [:functions, :macros] |> Enum.each(fn name -> 
    defmacro unquote(name)(option \\ :all) do 
     import MapSet 
     quote do 
     case unquote(option) do 
      x when x in [:a, :all]  -> __ENV__  |> Map.take([unquote(name)]) 
      x when x in [:d, :default] -> default_env |> Map.take([unquote(name)]) 
      x when x in [:i, :imported] -> 
      difference(new(Map.take(__ENV__, [unquote(name)])), 
         new(Map.take(default_env, [unquote(name)]))) 
      |> MapSet.to_list 
     end 
     end 
    end 
    end) 

end 

回答

3

你基本上需要解除引用两次,因为动态宏观一代已经是一个隐含的宏。你应该没问题在defmacro的顶部添加以下行:

name = unquote(name) 
相关问题