I have this code to generically transform String to Dto, if I am using Manifest and ClassTag, both of it I can use method runtimeClass to get runtime class, but TypeTag doesnt have this method
class ObjectMapper[T] {
def readValue(x: String, t: Class[T]): T = ???
}
class Reader {
def read[W: Manifest](x: String): W = {
val mapper = new ObjectMapper[W]
mapper.readValue(x, implicitly[Manifest[W]].runtimeClass.asInstanceOf[Class[W]])
}
}
May I know why TypeTag doesnt have method runtimeClass
Many thanks in advance
Assuming the TypeTag
comes from scala.reflect.runtime.universe
, you can get the class like this:
def runtimeClass(tag: TypeTag) = tag.mirror.runtimeClass(tag.tpe)
It doesn't have this method because not all TypeTag
s are from the runtime universe.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With