在下面的示例中,我想知道为什么funPoly
不能接受存在量化类型 value outersFromInnersEx
,即使funEx
可以。
case class InnerCassClass[I, E, O](i: I, e: E, o: O)
case class OuterCaseClass[I, E, O](inner: InnerCassClass[I, E, O])
val inner1 = InnerCassClass(5, "foo", 3.3f)
val inner2 = InnerCassClass(4.4f, 6, "bar")
// Doesn't work as expected due to invariance of type paramemters I, E, O (but confirm)
// val outersFromInnersAny: List[OuterCaseClass[Any, Any, Any]] = List(inner1, inner2).map(OuterCaseClass.apply)
val outersFromInnersEx: List[OuterCaseClass[_, _, _]] = List(inner1, inner2).map(OuterCaseClass.apply)
def funPoly[I, E, O](occ: List[OuterCaseClass[I, E, O]]): Unit = ()
def funEx(occ: List[OuterCaseClass[_, _, _]]): Unit = ()
// This doesn't work, but why?
val u1 = funPoly(outersFromInnersEx)
val u2 = funEx(outersFromInnersEx)
请注意,我在 Scala 3 中对此进行了测试(在线尝试),但问题在 Scala 2 中基本相同,尽管此特定示例在 Scala 2 中存在其他问题。