Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 如何对泛型类型中包含的类型变量进行大小写匹配?_Scala_Reflection_Pattern Matching - Fatal编程技术网

Scala 如何对泛型类型中包含的类型变量进行大小写匹配?

Scala 如何对泛型类型中包含的类型变量进行大小写匹配?,scala,reflection,pattern-matching,Scala,Reflection,Pattern Matching,我正在编写一个Scala子例程来构造一个TypeTag[Map[\up>这将简化为从一个TypeTag[Iterable[t1]获取一个TypeTag[t1]。这是可能的,但非常难看: 因此,您最终会得到类似(未经测试!)的结果: 导入scala.reflect.runtime.universe_ def typeToTypeTag[T]( tpe:类型, 镜像:reflect.api.mirror[reflect.runtime.universe.type] ):TypeTag[T]={ Ty

我正在编写一个Scala子例程来构造一个TypeTag[Map[\up>这将简化为从一个
TypeTag[Iterable[t1]
获取一个
TypeTag[t1]
。这是可能的,但非常难看:

因此,您最终会得到类似(未经测试!)的结果:

导入scala.reflect.runtime.universe_
def typeToTypeTag[T](
tpe:类型,
镜像:reflect.api.mirror[reflect.runtime.universe.type]
):TypeTag[T]={
TypeTag(镜像,新reflect.api.TypeCreator{
def应用[U]
隐式val t1=argTypeTag[t1](tt1)
隐式val t2=argTypeTag[t2](tt2)
typeTag[Map[t1,t2]]
}

这将简化为从
类型标签[Iterable[t1]]
获取
类型标签[t1]
。这是可能的,但非常难看:

因此,您最终会得到类似(未经测试!)的结果:

导入scala.reflect.runtime.universe_
def typeToTypeTag[T](
tpe:类型,
镜像:reflect.api.mirror[reflect.runtime.universe.type]
):TypeTag[T]={
TypeTag(镜像,新reflect.api.TypeCreator{
def应用[U]
隐式val t1=argTypeTag[t1](tt1)
隐式val t2=argTypeTag[t2](tt2)
typeTag[Map[t1,t2]]
}
tag1: TypeTag[Iterable[_<: A]]
tag2: TypeTag[Iterable[_<: B]]
(tag1, tag2) match {
  case (tt1: TypeTag[Iterable[t1]], tt2: TypeTag[Iterable[t2]]) =>
    implicit val t1 = tt1
    implicit val t2 = tt2
    ScalaReflection.universe.typeTag[Map[t1, t2]]
}
import scala.reflect.runtime.universe._

def typeToTypeTag[T](
  tpe: Type,
  mirror: reflect.api.Mirror[reflect.runtime.universe.type]
): TypeTag[T] = {
  TypeTag(mirror, new reflect.api.TypeCreator {
    def apply[U <: reflect.api.Universe with Singleton](m: reflect.api.Mirror[U]) = {
      assert(m eq mirror, s"TypeTag[$tpe] defined in $mirror cannot be migrated to $m.")
      tpe.asInstanceOf[U#Type]
    }
  })
}

def argTypeTag[A](tag: TypeTag[_ /* Actually F[A] for some F */]): TypeTag[A] = 
  typeToTypeTag[A](tag.tpe.typeArgs.head)

(tag1, tag2) match {
  case (tt1: TypeTag[Iterable[t1]], tt2: TypeTag[Iterable[t2]]) =>
    implicit val t1 = argTypeTag[t1](tt1)
    implicit val t2 = argTypeTag[t2](tt2)
    typeTag[Map[t1, t2]]
}