mardi 6 janvier 2015

Get containing generic types with ClassTag

I know that guava's TypeToken is capable of this:



class C[I, O]

class A[T] {
val tok = new TypeToken[T](getClass){}
}

val a = new A[C[Integer, String]]{}
println(a.tok) // somepackage.C<java.lang.Integer, java.lang.String>


But with scala's ClassTag, I get:



class B[T: ClassTag] {
// runtime class
val clazz = implicitly[ClassTag[T]].runtimeClass
// also tries to construct a guava type token
val tok = TypeToken.of(clazz).asInstanceOf[TypeToken[T]]
}

val b = new B[C[Integer, String]]
println(b.clazz) // class somepackage.C
println(b.tok) // somepackage.C


So with ClassTag, the genetic types contained by C are lost. Is it possible to get somepackage.C with ClassTag or other things in scala?


Thanks!






Aucun commentaire:

Enregistrer un commentaire