Introduction :
... TypeTag[T] encapsulates the representation of the runtime type of some type of compilation time T ....
... TypeTag always generated by the compiler .... [1]
TypeTag are in scala.reflect.** . Another SO answer mentions that using java reflection will put an excessive load on performance in your application.
Question :
To what extent TypeTag s, ClassTag and WeakTypeTag use java reflection at runtime? They are generated at compile time, but do they incur overhead at run time?
An example :
def isOfType[A : ClassTag : TypeTag, E : ClassTag : TypeTag](actual: A, expected: E): Boolean = { actual match { case _ : E if typeOf[A] =:= typeOf[E] => true case _ => false } } assert( isOfType(List.empty[Int], List.empty[Int])) assert(!isOfType(List.empty[String], List.empty[Int]))
Although tags are generated at compile time, I can feel a delay when it starts. Comparing types using inefficient java reflection under the hood?
reflection scala scala-reflect
mucaho
source share