在逆变位置使用协变类型有什么替代方案吗



我正在寻找一种从现有实例创建新实例的模式,这样我就可以通过遍历Tile上方的层次结构来计算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)您可以单独声明反向位置的类型,并在子类中实现:

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本身进行liskov替换。它将允许从Tile[Color]使用buildTile。但这种方法将baseTile[_ >: Color]结合起来,以满足对Tile[+A]本身的liskov替代:

scala> a.buildTile("", 1.0, a: Tile[Any]) //Tile[Any] also works
None
res27: Tile[Color] = BlueTile(,None)

因此,我建议将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])
                                   ^

相关内容

  • 没有找到相关文章

最新更新