[英]Elixir Macro issue with unquoting
你能看看我的微距吗? 我收到undefined function number/0
错误,我无法弄清原因。
defmodule DbUtil do
defmacro __using__(opts) do
quote do
import unquote(__MODULE__)
@before_compile unquote(__MODULE__)
end
end
defmacro __before_compile__(%{module: definition} = _env) do
quote do
import Ecto.Query
def last do
from x in unquote(definition), order_by: [desc: x.id], limit: 1
end
# This dumps error
def limits(number) do
from a in unquote(definition), limit: ^unquote(number)
end
end
end
end
您不需要unquote
number
。 当您想在quote
块外插入一个变量时,使用unquote
。 由于number
是在quote
定义的,因此您无需unquote
quote
。 以下应该为您工作:
def limits(number) do
from a in unquote(definition), limit: ^number
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.