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
時沒有意義。
有沒有辦法解決這個問題一般不訴諸方法和動態調度?