I can get a Type
from a TypeTag[A]
by using the tpe
method. But can I also recover the type-tag from a type?
import scala.reflect.runtime.{universe => ru}
import ru.{Type, TypeTag}
def forward[A](implicit tt: TypeTag[A]): Type = tt.tpe
def backward(t: Type): TypeTag[_] = ???
The reason being that I have an API that uses type-tags as keys into a map, but at some point I only have the type and dropped the tag.
A TypeTag is completely compiler-generated, that means that the compiler creates and fills in a TypeTag when one calls a method expecting such a TypeTag . There exist three different forms of tags: scala. reflect. ClassTag.
A ClassTag[T] stores the erased class of a given type T , accessible via the runtimeClass field. This is particularly useful for instantiating Array s whose element types are unknown at compile time. ClassTag s are a weaker special case of scala. reflect. api.
Scala reflection enables a form of metaprogramming which makes it possible for programs to modify themselves at compile time. This compile-time reflection is realized in the form of macros, which provide the ability to execute methods that manipulate abstract syntax trees at compile-time.
It is possible:
import scala.reflect.runtime.universe._
import scala.reflect.api
val mirror = runtimeMirror(getClass.getClassLoader) // whatever mirror you use to obtain the `Type`
def backward[T](tpe: Type): TypeTag[T] =
TypeTag(mirror, new api.TypeCreator {
def apply[U <: api.Universe with Singleton](m: api.Mirror[U]) =
if (m eq mirror) tpe.asInstanceOf[U # Type]
else throw new IllegalArgumentException(s"Type tag defined in $mirror cannot be migrated to other mirrors.")
})
assert(backward[String](forward[String]) == typeTag[String])
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