我想在引用块内部使用代码块内定义的变量来调用一个私有宏。以下是伪代码(无法正常工作):
defmodule Foo do
defmacrop debug(msg) do
quote bind_quoted: [msg: msg], do: IO.puts(msg)
end
defmacro __using__(_) do
quote do
def hello do
my = "testme"
unquote(debug(quote do: my))
end
end
end
end
defmodule Bar do
use Foo
end
Bar.hello()
而这会在编译时被转换(在我的脑海中)为:
defmodule Bar do
def hello do
my = "testme"
IO.puts(my)
end
end
有没有办法实现这个?我在寻找相关文档方面遇到了困难。
更新
我发现:
defmodule Foo do
defmacrop debug() do
quote do: IO.puts("hello")
end
defmacro __using__(_) do
quote do
def hello do
my = "testme"
unquote(debug())
end
end
end
end
这段内容的意思是要将变量正确地转换成我需要的形式,但我在努力寻找一种方法来传递变量本身,以便它变成IO.puts(my)