我试图用下面的代码定义两个宏,但** (CompileError) iex:12: undefined function name/0失败了。函数参数name不能在defmacro的do块中不引用。
原因是什么?有什么办法解决这个问题吗?
(灵丹妙药版本为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发布于 2016-05-15 10:12:15
您基本上需要取消引用两次,因为动态宏生成已经是一个隐式宏。您应该可以在defmacro的顶部添加以下行
name = unquote(name)https://stackoverflow.com/questions/37236450
复制相似问题