![](/img/trans.png)
[英]Macro expansion in elixir: how to define 2 macros with one using the other?
[英]Elixir: How to define multiple macros using a List as a variable?
我正在尝试使用以下代码定义两个宏但它失败了** (CompileError) iex:12: undefined function name/0
。 函数参数name
不能在defmacro
的do块中defmacro
。
这是什么原因? 有什么方法可以解决这个问题吗?
(Elixir版本是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
您基本上需要取消引用两次,因为动态宏生成已经是隐式宏。 你可以在你的defmacro
顶部添加以下行:
name = unquote(name)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.