Scala - runtime performance of TypeTags, ClassTags and WeakTypeTags

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?

+7
reflection scala scala-reflect
source share
1 answer

Well, you can look here . In your case, Java reflection is not involved, but =:= ultimately delegates to isSameType2 , which is pretty nontrivial. He first checks referential equality.

+3
source share

All Articles