宏块意外返回[do:return value]

时间:2018-06-10 07:51:20

标签: macros elixir metaprogramming

我在宏中有以下定义:

defmacro route(pattern, options \\ [], block) do
  name = "my_fun"
  def unquote(name)(var!(conn)) do
    unquote(block)
  end
end

我用这个函数调用宏:

route "/" do
  conn
  |> put_resp_content_type("text/plain")
  |> send_resp(200, "Hello, world!")

内部块正在返回一个%Plug.Conn{}结构,但是当我在我的宏中打印unquote(block)的结果时,我得到[do: %Plug.Conn{}]我做错了什么? do关键字列表来自何处?

更大的片段

defmacro route(pattern, options \\ [], block) do
  name = Keyword.get_lazy(options, :name, fn -> create_name(pattern, options) end)
  route = {pattern, name}
  quote do
    @routes [unquote(route) | @routes]
    def unquote(name)(var!(conn)) do
      resp = unquote(block)
      IO.puts("outside the block, resp is")
      IO.inspect(resp)
    end
  end
end
route "/" do
  resp = conn
  |> put_resp_content_type("text/plain")
  |> send_resp(200, "Hello, world!")
  IO.puts("Inside the block, resp is")
  IO.inspect(resp)
  resp
end

结果

Inside the block, resp is
%Plug.Conn{
  adapter: {Plug.Adapters.Cowboy2.Conn, :...},
  assigns: %{},
  {....SNIP....}
}

outside the block, resp is
[
  do: %Plug.Conn{
    adapter: {Plug.Adapters.Cowboy2.Conn, :...},
    assigns: %{},
    {....SNIP....}
  }
]

1 个答案:

答案 0 :(得分:1)

您需要将参数从block更改为do: block

defmacro route(pattern, options \\ [], do: block) do

Elixir宏调用中的do ... end参数作为一项关键字列表传递,其中键为:do

如果您没有通过将其匹配为do: block的模式删除该嵌套,则参数的值将为[do: block],当您取消引用它时,您将取消引用[do: ...],这也是一个有效的表达式,并解释了为什么您的代码有效以及为什么会打印[do: ...]