0
class Parent[T: ClassTag] {
}

class Child[U: ClassTag, T: ClassTag] extends Parent[T] {
}

val o: Parent[_] = new Child[Int, String]

Is it possible to get the actual types of T and U given o (note its type Parent[_]), assuming you know o is of type Child?

I have tried a few things based on Runtime resolution of type arguments using scala 2.10 reflection but no luck so far.

Community
  • 1
  • 1
savx2
  • 1,011
  • 2
  • 10
  • 28

1 Answers1

0

If you modify Parent and Child slightly (note that : ClassTag notation is basically the same except for val, so it doesn't give a name to the implicit parameter or make it accessible from outside the class)

class Parent[T](implicit val tTag: ClassTag[T]) {
}

class Child[U, T](implicit override val tTag: ClassTag[T], val uTag: ClassTag[U]) extends Parent[T] {
}

then you can do

val tTag = o.tTag // ClassTag[String]

Of course, if you need uTag, you'll have to cast: o.asInstanceOf[Child[_, _]].uTag, but it'll work

assuming you know o is of type Child

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487