【发布时间】:2016-10-31 13:50:17
【问题描述】:
如何将关键字列表发送到宏并使用bind_quoted?这是一个示例:
带有宏的模块:
defmodule MacroTime do
defmacro __using__(opts) do
quote bind_quoted: [opts: opts] do
def from_opts do
# Using opts here produces an undefined function error
IO.puts(opts[:foo])
end
end
end
end
导入模块和脚本:
defmodule Main do
use MacroTime, foo: "bar"
end
Main.from_opts
运行它会产生:
** (CompileError) main.ex:2: undefined function opts/0
您可以在这里试用:https://glot.io/snippets/eg2gg4huj3
我觉得我缺少一些关于宏的简单概念。
【问题讨论】:
-
试试
unquote(opts)[:foo]。 -
@Dogbert 是的,没有引用绑定也可以工作,但我真的想在引用块的几个地方使用 opts[:foo] 并希望避免多次取消引用,因此 @ 987654327@