According to scala doc, TypeTag contains more information than ClassTag. It seems to me that TypeTag can do more than ClassTag, for example, deliver information about parameters such as compile time at runtime, etc.
However, the following example shows that a ClassTag can do the job, but a TypeTag cannot. I want to understand why.
import scala.reflect.ClassTag import scala.reflect.runtime.universe.TypeTag // def func[T](o: Any): Unit = { // def func[T : TypeTag](o: Any): Unit = { def func[T : ClassTag](o: Any): Unit = { o match { case x: T => println(Some(x)) case _ => println(None) }spark } func[Map[Int, Int]](List(1, 2, 3))
Only a ClassTag will cause the templates to match None (which is the expected behavior), the first two lines with comments will appear with the Some branch.
It seems that a ClassType can reflect the type of an object at runtime, while a Type Tag cannot. But is TypeTag a superset of ClassTag? I would like to know the explanation as detailed as possible. Thanks.
source share