I want to create a function that returns the runtime representation of Scala subclasses that have the same superclass using classOf
, e.g.
class C
class C0 extends C
class C1 extends C
def f(i: Int): Class[C] = {
if (i % 2 == 0) classOf[C0]
else classOf[C1]
}
However, the return values of classOf[Cn]
give me the error Expression of type classOf[Cn] doesn't confirm to the expected type Class[C], giving me the impression that inheritance information is lost in Class[T]
.
I reckon ClassTag
s could somehow help in retaining the ereased type, but how?