Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why TypeTag doesnt have method runtimeClass but Manifest and ClassTag do

Tags:

scala

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

like image 881
Xiaohe Dong Avatar asked Oct 13 '14 11:10

Xiaohe Dong


1 Answers

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 TypeTags are from the runtime universe.

like image 160
Alexey Romanov Avatar answered Oct 06 '22 01:10

Alexey Romanov