Scala 从字符串创建typeTag并将其传递给多态方法

Scala 从字符串创建typeTag并将其传递给多态方法,scala,generics,reflection,Scala,Generics,Reflection,我有一个多态方法,它将自定义case类作为类型参数。 现在,为了支持几个case类(在配置文件中定义为字符串),我需要将字符串转换为case类的tagType 为此,我使用runtimeMirror方法从字符串中获取类, 然后我使用manifestToTypeTag来获取tagType() 导入scala.reflect.runtime.universe_ 导入scala.reflect.runtime.universe 导入scala.reflect.ManifestFactory //我的多

我有一个多态方法,它将自定义case类作为类型参数。 现在,为了支持几个case类(在配置文件中定义为字符串),我需要将字符串转换为case类的
tagType

为此,我使用
runtimeMirror
方法从
字符串中获取类,
然后我使用
manifestToTypeTag
来获取
tagType
()

导入scala.reflect.runtime.universe_
导入scala.reflect.runtime.universe
导入scala.reflect.ManifestFactory
//我的多态方法
def printMe[T](l:List[T])(隐式类型标签:类型标签[T]):单位=println(l)
//这项工作:
printMe(List(“fdfg”)(typeTag[java.lang.String])
//现在,我想从字符串动态构建typeTag
val className=“java.lang.String”//a自定义案例类
val mirror=universe.runtimeMirror(getClass.getClassLoader)
val cls=Class.forName(className)
//从类名中获取typeTag
val t=internal.manifestToTypeTag(镜像,ManifestFactory.classType(cls))
//使用生成的typeTag调用方法
打印时间(列表(“fdfg”)(t)
//编译错误
错误:(12,31)类型不匹配;
找到:scala.reflect.api.Universe#TypeTag[无]
必需:reflect.runtime.universe.TypeTag[字符串]
注意:无请尝试以下建议:

def printMe[T](l:List[T])(隐式typeTag:typeTag[T]):Unit=println(l)
def stringToTypeTag[A](名称:String):TypeTag[A]={
val c=类。forName(名称)
val mirror=runtimeMirror(c.getClassLoader)
val sym=mirror.staticClass(名称)
val tpe=sym.selfType
TypeTag(镜像,新api.TypeCreator{
def应用[U]
import scala.reflect.runtime.universe._
import scala.reflect.runtime.universe
import scala.reflect.ManifestFactory

// My polymorphic method
def printMe[T](l: List[T])(implicit typeTag: TypeTag[T]): Unit = println(l)

// This works:
printMe(List("fdfg"))(typeTag[java.lang.String])

// Now, I want to build the typeTag dynamically from a String
val className = "java.lang.String" // a Custom case class 
val mirror = universe.runtimeMirror(getClass.getClassLoader)
val cls = Class.forName(className)
// Getting the typeTag from the class name
val t = internal.manifestToTypeTag(mirror,ManifestFactory.classType(cls))

// Call of the method with the generated typeTag
printMe(List("fdfg"))(t)

// Compilation error
Error:(12, 31) type mismatch;
found   : scala.reflect.api.Universe#TypeTag[Nothing]
required: reflect.runtime.universe.TypeTag[String]
Note: Nothing <: String, but trait TypeTag is invariant in type T.
You may wish to investigate a wildcard type such as `_ <: String`. (SLS 3.2.10)
printMe(List("fdfg"))(t)
def printMe[T](l: List[T])(implicit typeTag: TypeTag[T]): Unit = println(l)

def stringToTypeTag[A](name: String): TypeTag[A] = {
  val c = Class.forName(name)
  val mirror = runtimeMirror(c.getClassLoader)
  val sym = mirror.staticClass(name)
  val tpe = sym.selfType
  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.")
  })
}

printMe(List("fdfg"))(stringToTypeTag("java.lang.String"))