2016-05-15 52 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) 
相關問題