« Return to Thread: [scala] Covariance in unsound

[scala] Covariance in unsound

by nikov :: Rate this Message:

Reply to Author | View in Thread

Hi,

It seems that the current implementation of covariance is unsound.
Consider this code:

///////////////////////////////////////////////
class B {
  type X
  var x : X = _
}
class C[+T] {
  val s = new B { type X = T }
}
///////////////////////////////////////////////

It compiles without errors in Scala 2.8.0.r18050-b20090618020144, but
then we can write:

scala> val c = new C[Int]
c: C[Int] = C@1046270

scala> val c1 = c : C[Any]
c1: C[Any] = C@1046270

scala> c1.s.x = ""

scala> c.s.x
java.lang.ClassCastException: java.lang.String cannot be cast to
java.lang.Integer
        at scala.runtime.BoxesRunTime.unboxToInt(Unknown Source)


I would like to read some papers on theoretical foundations of
covariance in Scala. What would you recommend?

Thanks,
Vladimir

 « Return to Thread: [scala] Covariance in unsound