【发布时间】:2016-06-05 16:32:52
【问题描述】:
我正在编写一个 Scala 子例程来从 2 个类型标签构造一个 TypeTag[Map[_<: a _ b>
tag1: TypeTag[Iterable[_<: A]]
tag2: TypeTag[Iterable[_<: B]]
上限是必要的,因为 A 和 B 都被声明为协变,而 TypeTag[T] 是不变的。
我正在尝试使用大小写匹配来获取 A 和 B 的隐式类型变量,但发现 Scala 中的类型模式匹配相对较周。即以下代码编译失败:
(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]]
}
因为 t1 和 t2 无法解析。我应该怎么做才能解决这个问题?
【问题讨论】:
标签: scala reflection pattern-matching