Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a TypeTag from a Type?

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.

like image 523
0__ Avatar asked Jan 11 '15 13:01

0__


People also ask

What is TypeTag?

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.

What is ClassTag in Scala?

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.

What is Scala reflect?

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.


1 Answers

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])
like image 106
Vladimir Matveev Avatar answered Nov 13 '22 19:11

Vladimir Matveev