Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala lower type bounds and covariance

Im reading this page http://www.scala-lang.org/node/137, I understand what covariance is and lower bounds as well, but what it's not clear is this line:

Unfortunately, this program does not compile, because a covariance annotation is only possible if the type variable is used only in covariant positions. Since type variable T appears as a parameter type of method prepend, this rule is broken.

why elem has to be an instance of a supertype of T, if ListNode is already covariant why elem cannot be prepended to the current list.

like image 509
loki Avatar asked Nov 05 '22 00:11

loki


1 Answers

class Super             {override def toString = "Super"}
class Sub extends Super {override def toString = "Sub"; def subMethod {} }
val sup = new Super
val sub = new Sub

Imagine the following were allowed:

// invalid code
class Foo[+T] {
  def bar(x: T) = println(x)
}

Since Foo is covariant on T, this is valid (a simple upcast, since a Foo[Sub] is a Foo[Super]):

val foo : Foo[Super] = new Foo[Sub] {
  override def bar(x: Sub) = x.subMethod
}

Now foo is, as far as we know, a Foo[Super] like any other, but its bar method won't work, because the bar implementation requires a Sub:

foo.bar(sup) // would cause error!
like image 82
Luigi Plinge Avatar answered Nov 15 '22 05:11

Luigi Plinge