Consider a type hierarchy, where the base object is non-generic, but sub-types are:
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"
Using the of
operator like this won't compile, because it expects object types. What does work is e.g. to match for specific types like DerivedB[int]
, or making the proc itself generic in T
, which is meaningless when passing in a DerivedA
.
Is there a way to solve this problem generically without resorting to methods and dynamic dispatch?