在 Scala 中,如何创建 TypeTag[Map[A,B]],而只有 TypeTag[A] 和 TypeTag[B] 可用?

In Scala, How to create TypeTag[Map[A,B]], while only TypeTag[A] and TypeTag[B] are available?

假设类型 A 和 B 在 运行 时都被擦除且未知。

有没有办法构造TypeTag[Map[A,B]]?

最好只使用显式构造函数,因为在我的真实代码中,A 和 B 都是通配符(可以将类型参数分配给调用它们的 2 个函数,但是当它们只使用一次时为什么还要提取 2 个方法)。

谢谢你的想法。任何建议表示赞赏。

Preferrably using only explicit constructor, as in my real code both A and B are wildcard

不是很清楚,但是你的意思是你有tagA: TypeTag[_]tagB: TypeTag[_]?如果是这样,那么你可以做

(tagA, tagB) match {
  case (tagA: TypeTag[a], tagB: TypeTag[b]) =>
    implicit val tagA1 = tagA
    implicit val tagB1 = tagB
    typeTag[Map[a, b]]
}

有些丑陋和陈词滥调,但我认为目前没有更好的方法(并且很乐意学习其他方法)。