Skip to content

Instantly share code, notes, and snippets.

@mpilquist
Created March 17, 2013 14:14
Show Gist options
  • Save mpilquist/5181685 to your computer and use it in GitHub Desktop.
Save mpilquist/5181685 to your computer and use it in GitHub Desktop.
Getting type tags for type parameters to type constructors
import scala.reflect._
import scala.reflect.api._
def innerTypeTags[A: TypeTag]: List[TypeTag[_]] = {
val typeParams = typeTag[A].tpe match {
case TypeRef(_, _, args) => args
}
typeParams map { tpe =>
TypeTag(typeTag[A].mirror, new TypeCreator {
override def apply[U <: Universe with Singleton](m: scala.reflect.api.Mirror[U]): U # Type =
if (m eq typeTag[A].mirror) tpe.asInstanceOf[U#Type] else sys.error("Wrong mirror")
})
}
}
innerTypeTags[List[Int]] // List[reflect.runtime.universe.TypeTag[_]] = List(TypeTag[Int])
innerTypeTags[Map[Int, String]] // List[reflect.runtime.universe.TypeTag[_]] = List(TypeTag[Int], TypeTag[String])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment