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