lundi 29 décembre 2014

Guava TypeToken in scala

I'm working with a java lib in scala.


I need to impl an abstract class in scala but there's some problem with guava's TypeToken which is used in the abstract class in java. The problem is that sometimes in scala, TypeToken is not able to infer the generic type:



import com.google.common.reflect.TypeToken


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


object TypeTokenTest extends App {
val ok = new SomeClass[String]{}
println(ok.tok) // `java.lang.String`, OK!!

def wrap[O]() = {
new SomeClass[O]{}
}

val notOk = wrap[String]()
println(notOk.tok) // `O`, not Ok ...
}


So in above code, if it's a simple usage, it works very well. But if there's an indirection (the concrete type is passed from the function to the class), it does not work anymore.


Any suggestions on how to make it work?






Aucun commentaire:

Enregistrer un commentaire