1
考虑一个类型层次结构,其中的基本对象是不通用的,但子类型是:如何在泛型中使用Nim的````运算符?
type
TestBase = ref object of RootObj
DerivedA = ref object of TestBase
DerivedB[T] = ref object of TestBase
field: T
proc testProc(x: TestBase) =
if x of DerivedB: # <= what to do here
echo "it's a B!"
else:
echo "not a B"
使用of
运营商这样不会编译,因为它要求object类型。什么是工作,例如以匹配DerivedB[int]
之类的特定类型,或者在T
中使处理程序本身具有通用性,这在通过DerivedA
时没有意义。
有没有办法解决这个问题一般不诉诸方法和动态调度?