macros - bind_quoted 不会在 def 内创建绑定
问题描述
bind_quoted
似乎对我不起作用。这是一个不使用的示例,bind_quoted
它按预期工作:
defmodule Animals do
defmacro dog do
x = 4
quote do
def go do
IO.puts unquote(x)
end
end
end
end
defmodule Test do
require Animals
Animals.dog #inject the definition of go() into the Test module
end
在 iex 中:
iex(10)> c "a.exs"
warning: redefining module Animals (current version defined in memory)
a.exs:1
warning: redefining module Test (current version defined in memory)
a.exs:15
[Test, Animals]
iex(11)> Test.go
4
:ok
iex(12)>
但是bind_quoted 文档说:
... :bind_quoted 选项建议在每次希望将值注入引用时使用。
好的,让我们保持一致:
defmodule Animals do
defmacro dog do
x = 4
quote bind_quoted: [x: x] do
def go do
IO.puts x
end
end
end
end
defmodule Test do
require Animals
Animals.dog #inject go() into the Test module
end
在iex中编译:
iex(10)> c "a.exs"
warning: redefining module Animals (current version defined in memory)
a.exs:1
warning: redefining module Test (current version defined in memory)
a.exs:15
warning: variable "x" does not exist and is being expanded to "x()", please use parentheses to remove the ambiguity or change the variable name
a.exs:17
== Compilation error in file a.exs ==
** (CompileError) a.exs:17: undefined function x/0
(stdlib) lists.erl:1338: :lists.foreach/2
(stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
** (CompileError) compile error
(iex) lib/iex/helpers.ex:183: IEx.Helpers.c/2
iex(10)>
错误报告中的相关消息是:
warning: variable "x" does not exist
为什么不?
解决方案
通常,是的。这就是它的工作方式。但是def
调用本身是一个宏,所以你仍然需要unquote
在它里面使用。如果您直接引用IO.puts
,它将毫无问题地工作。
这是演示它的代码的略微修改版本:
defmodule Animals do
defmacro dog do
x = 4
quote(bind_quoted: [xx: x]) do
IO.puts(xx)
end
end
end
defmodule Test do
require Animals
def go do
Animals.dog
end
end
现在回到你的实现;我一定x
要xx
在这个例子中明确地告诉你,如果你试图在x
这里取消引用(而不是xx
),它会抛出一个编译错误:
defmodule Animals do
defmacro dog do
x = 4
quote(bind_quoted: [xx: x]) do
def go do
IO.puts(unquote(xx))
end
end
end
end
推荐阅读
- odoo-12 - 无法在odoo中确认销售订单
- php - 如何从 Microsoft Graph API 获取刷新令牌
- ios - PHAsset Null 但本地标识符有效
- java - 如何检查 Currency.getAvailableCurrencies() 是否包含我的字符串值?
- java - 怎么会有 reflect.field 库中一个 Field 的值?
- javascript - 在继续执行脚本之前获取函数的结果
- excel - Power Query:合并一个文件中的两个表并为一个文件夹中的所有文件重复过程?
- python - Why is python interpreter not working on Zeppelin 0.8.0 for any of my notebooks?
- javascript - 单击按钮侦听器后如何调用预加载?
- shopware - Shopware 默认产品滑块的附加条件