在逆变位置使用协变型是否有其他选择?

时间:2015-02-11 05:29:36

标签: scala generics covariance contravariance

我正在寻找一种从现有实例创建新实例的模式,这样我就可以通过遍历它上面的层次结构来计算Tile的ultimateBase。我尝试了以下操作,但在 buildTile 方法的参数 base 上得到“协变类型A出现在逆变位置”。是否有一种替代模式来构建依赖于先前实例的实例?

trait Color
trait Blue extends Color
trait Green extends Color

trait Tile[+A] {
  def declaredBase: Option[(Double, Tile[A])]

  final val ultimateBase: Option[(Double, Tile[A])] = ??? // Some implementation, not important

  // Can't be protected[this]
  def buildTile(name: String, multiple: Double, base: Tile[A]): Tile[A]
}

case class BlueTile(name: String, declaredBase: Option[(Double, Tile[Blue])]) extends Tile[Blue] {
  override def buildTile(name: String, multiple: Double, base: Tile[Blue]): Tile[Blue] = {
    // Something more complicated here
    this
  }
}

谢谢。

1 个答案:

答案 0 :(得分:3)

1)你可以分别声明逆变位置的类型,并在子类中实现它:

trait Tile[+A] {
  type T <: A
  def declaredBase: Option[(Double, Tile[A])]

  final val ultimateBase: Option[(Double, Tile[A])] = None // Some implementation, not important

  // Can't be protected[this]
  def buildTile(name: String, multiple: Double, base: Tile[T]): Tile[A]
}

case class BlueTile(name: String, declaredBase: Option[(Double, Tile[Blue])]) extends Tile[Blue] {
  type T = Blue
  override def buildTile(name: String, multiple: Double, base: Tile[T]): Tile[Blue] =
  {
    println(base.declaredBase)// Something more complicated here
    this
  }
}

scala> val x = new BlueTile("", None)
x: BlueTile = BlueTile(,None)

scala> val a: Tile[Color] = new BlueTile("", None).buildTile("", 1.0, x) //covariance works
None
a: Tile[Color] = BlueTile(,None)

它适用于这种特殊情况,因为你最终可以指定类型T.

2)另一种方法是隐式添加def buildTile

scala> :paste
// Entering paste mode (ctrl-D to finish)

trait Tile[+A] {  
  def declaredBase: Option[(Double, Tile[A])]
  final val ultimateBase: Option[(Double, Tile[A])] = None // Some implementation, not important 
}

case class BlueTile(name: String, declaredBase: Option[(Double, Tile[Blue])]) extends Tile[Blue]

implicit class BuildFromBlue(t: Tile[Blue]) {
  def buildTile(name: String, multiple: Double, base: Tile[Blue]): Tile[Blue] = {
    println(base.declaredBase)// Something more complicated here
    t
  }
}

// Exiting paste mode, now interpreting.

defined trait Tile
defined class BlueTile
defined class BuildFromBlue

scala> val x = new BlueTile("", None)
x: BlueTile = BlueTile(,None)

scala> val a: Tile[Color] = new BlueTile("", None).buildTile("", 1.0, x) //covariance works
None
a: Tile[Color] = BlueTile(,None)

3A)最后的选择是通过存在类型的协方差。您可以将Tile[A]声明为不变量,但在需要时需要Tile[_ <: A]

trait Tile[A] {
  def declaredBase: Option[(Double, Tile[A])]
  def buildTile(name: String, multiple: Double, base: Tile[A]): Tile[A]
}

case class BlueTile(name: String, declaredBase: Option[(Double, Tile[Blue])]) extends Tile[Blue] {
  override def buildTile(name: String, multiple: Double, base: Tile[Blue]): Tile[Blue] = this
}

scala> val x = new BlueTile("", None)
x: BlueTile = BlueTile(,None)

scala> val a: Tile[_ <: Color] = new BlueTile("", None).buildTile("", 1.0, x)
a: Tile[_ <: Color] = BlueTile(,None)

1,2,3A)但是在投射到较大类型Tile[Color]之后,你无法在逆变位置使用它:

scala> a.buildTile("", 1.0, a)
<console>:18: error: type mismatch;
found   : Tile[_$1(in value res20)] where type _$1(in value res20) <: Color
required: Tile[_$1(in value a)]
          a.buildTile("", 1.0, a)
                               ^

3B)您可以将base与逆变存在类型绑定以实现:

trait Tile[+A] {
  def declaredBase: Option[(Double, Tile[A])]
  def buildTile(name: String, multiple: Double, base: Tile[_ >: A]): Tile[A]
}

case class BlueTile(name: String, declaredBase: Option[(Double, Tile[Blue])]) extends Tile[Blue] {
  override def buildTile(name: String, multiple: Double, base: Tile[_ >: Blue]): Tile[Blue] = {println(base.declaredBase); this}
}

scala> val x = new BlueTile("", None)
x: BlueTile = BlueTile(,None)

scala> val a: Tile[Color] = new BlueTile("", None).buildTile("", 1.0, x: Tile[Blue])
None
a: Tile[Color] = BlueTile(,None)

scala> a.buildTile("", 1.0, a) //you can do it now
None
res23: Tile[Color] = BlueTile(,None)

它的工作原理[_ >: A]实际上表示您不需要base buildTile本身。它允许Tile[Color]使用base。但是这种方法会将Tile[_ >: Color]解除绑定到Tile[+A]以满足scala> a.buildTile("", 1.0, a: Tile[Any]) //Tile[Any] also works None res27: Tile[Color] = BlueTile(,None) 本身的liskov替换:

A

所以我建议将Color限制在Tile(或者可能在子标记trait Tile[+A <: Color]{...} ... scala> a.buildTile("", 1.0, a) None res33: Tile[Color] = BlueTile(,None) scala> a.buildTile("", 1.0, a: Tile[Any]) <console>:18: error: type arguments [Any] do not conform to trait Tile's type parameter bounds [+A <: Color] a.buildTile("", 1.0, a: Tile[Any]) ^ 中的某处):

{{1}}