在 Scala 中,协变类型参数怎么可能是抽象类型的上限,而不是另一个不变类型参数?

问题描述

下面的代码演示得很清楚:

trait poly1Group[-IUB,+OUB] {

  trait Case[-I <: IUB] {

    type Out <: OUB // <------------------------------- success

    def apply(v: I): Out

  }

  trait ==>[
      -I <: IUB,O <: OUB //<------------------------------------- fail
  ] extends Case[I] {

    final type Out = O
  }

  def apply[I <: IUB](v: I)(implicit ev: Case[I]): ev.Out = ev(v)
}

需要注意的是,OutO 都是不变的,因此它们不应该对 OUB 的方差施加任何约束。然而一个成功一个失败:

[Error] .../poly1Group.scala:16: covariant type OUB occurs in contravariant position in type  <: OUB of type O
one error found

怎么可能?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)