我正在尝试编写一个宏,其中除其他外,它为将在宏外部定义的过程提供前向定义。但是,我收到错误。 (这与我在为什么我调用 nim 模板不是简单地扩展模板代码?而是使用宏而不是模板中遇到的问题类似)。
具体来说我有以下代码
import macros
macro General_Macro*(vType: untyped) =
quote do:
proc Value*(self:var `vType`): float
template General_Template*(vType: untyped) {.dirty.} =
proc Value*(self:var vType): float
type
Test_Type = ref object of RootObj
General_Macro(Test_Type)
#General_Template(Test_Type)
proc Value(self: var Test_Type): float =
return 0.0
var Element = new(Test_Type)
echo $Element.Value
我得到了错误
Test/test3x.nim(42, 14) Error: ambiguous call; both test3x.Value(self`gensym0: var Test_Type) [proc declared in Test/test3x.nim(25, 14)] and test3x.Value(self: var Test_Type) [proc declared in Test/test3x.nim(37, 6)] match for: (Test_Type)
但是,如果我取消注释
General_Template(Test_Type)
并注释掉 General_Macro(Test_Type)
,程序将编译并运行,不会出现任何问题。
我不确定为什么宏中的前向定义没有被后面的定义识别。有人可以帮助解释我缺少什么吗?
很像
template
s quote
是一个卫生的准报价宏。因此,报价过程定义中需要 self {.inject.}
。