Scala类型系统(sudden thought)

  http://docs.scala-lang.org/tour/lower-type-bounds.html中有一段代码

trait Node[+B] {
  def prepend(elem: B): Unit
}

case class ListNode[+B](h: B, t: Node[B]) extends Node[B] {
  def prepend(elem: B) = ListNode[B](elem, this)
  def head: B = h
  def tail = t
}

case class Nil[+B]() extends Node[B] {
  def prepend(elem: B) = ListNode[B](elem, this)
}

  文中说这段代码不会通过编译,因为Function1是contravariant 在参数的位置上。看到这里是一个头很多个大的。 However, this program does not compile because the parameter elem in prepend is of type B, which we declared covariant. This doesn’t work because functions are contravariant in their parameter types and covariant in their result types.

  先假设一下如果能编译通过的话。

  假设有这样子的一段代码

trait Animal
case class Dog() extends Animal
case class Cat() extends Animal
def addDogToAnimal(animalNode : ListNode[Animal]) : Unit{
    animalNode.prepend(Dog())
}
如果generic的类型是Animal的话,ListNode就变成如下
case class ListNode[Animal](h: Animal, t: Node[Animal]) extends Node[Animal] { def prepend(elem: Animal) = ListNode[Animal](elem, this) def head: Animal = h def tail = t }
如果generic的类型是Cat的话,ListNode就变成如下
case class ListNode[Cat](h:Cat, t: Node[Cat]) extends Node[Cat] { def prepend(elem:Cat) = ListNode[Cat](elem, this) def head: Cat= h def tail = t }

 addDogToAnimal方法接受一个ListNode[Animal],因为ListNode[Cat] 是 ListNode[Animal]的子类(因为是Covaraiance的)

 所以我们可以addDogToAnimal(ListNode(Cat(), Nil())),但是ListNode[Cat]只能prepend是Cat类型的对象。所以一定会出问题。

 解决方法就是在所有需要消费者方法中 introducing a new type parameter U that has B as a lower type bound.

trait Node[+B] {
  def prepend[U >: B](elem: U)
}

case class ListNode[+B](h: B, t: Node[B]) extends Node[B] {
  def prepend[U >: B](elem: U) = ListNode[U](elem, this)
  def head: B = h
  def tail = t
}

case class Nil[+B]() extends Node[B] {
  def prepend[U >: B](elem: U) = ListNode[U](elem, this)
}
现在再来看刚才的问题

如果generic的类型是Animal的话,ListNode就变成如下
case class ListNode[Animal](h: Animal, t: Node[Animal]) extends Node[Animal] {
  def prepend[U >: Animal](elem: Animal) = ListNode[Animal](elem, this)
  def head: Animal = h
  def tail = t
}
如果generic的类型是Cat的话,ListNode就变成如下
case class ListNode[Cat](h:Cat, t: Node[Cat]) extends Node[Cat] { def prepend[U >: Cat](elem:Cat) = ListNode[Cat](elem, this) def head: Cat= h def tail = t }
ListNode[Cat] 还是 ListNode[Animal]的子类
addDogToAnimal(ListNode(Cat(), Nil()))的时候
ListNode[Cat]的prepend方法可以接受所有U >: Cat 的对象
所以prepend方法可以接受Animal的对象作为参数。Dog也是一种Animal,所以
animalNode.prepend(Dog())是没有问题的
在这里以一个java开发者来说,会觉得很不合理。明明是cat类型的ListNode,怎么可以加入dog。但这也是scala和java的不同呀。唉


 

 

  

posted @ 2017-10-15 23:49  哇哩顾得  阅读(540)  评论(0编辑  收藏  举报