首页 > 解决方案 > 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

为什么不?

标签: macroselixir

解决方案


通常,是的。这就是它的工作方式。但是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

现在回到你的实现;我一定xxx在这个例子中明确地告诉你,如果你试图在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

推荐阅读