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
答案 0 :(得分:2)
您无需unquote
number
。当您想要注入unquote
块之外的变量时,使用quote
。由于在number
内定义了quote
,因此您无需unquote
。以下内容适用于您:
def limits(number) do
from a in unquote(definition), limit: ^number
end