« Return to Thread: [scala] Structural types within classes with contravariant type parameters are unsound

Re: [scala] Structural types within classes with contravariant type parameters are unsound

by Vlad Patryshev :: Rate this Message:

Reply to Author | View in Thread

Do you imply variance can and should be checked right away? That would be just great (but may cause a lot of frustration among developers unless there's a proper error message).

2009/6/18 Vladimir Reshetnikov <v.reshetnikov@...>
Hi,

Consider the following code:
////////////////////////////////////////
class A[-S](y : S) {
 val f  = new { val x = y }
}
////////////////////////////////////////

It compiles without any errors. But now we can write:

scala> val a = new A(1)
a: A[Int] = A@43e3ac

scala> val b = a : A[Nothing]
b: A[Nothing] = A@43e3ac

scala> b.f.x
java.lang.ClassCastException: java.lang.Integer cannot be cast to
scala.runtime.Nothing$
       at .<init>(<console>:8)
       at .<clinit>(<console>)
       at RequestResult$.<init>(<console>:4)
       at RequestResult$.<clinit>(<console>)
       at RequestResult$result(<console>)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorIm...

Looks like unsoundness.

Thanks,
Vladimir



--
Thanks,
-Vlad

 « Return to Thread: [scala] Structural types within classes with contravariant type parameters are unsound