« Return to Thread: [scala] Structural refinements containing methods with higher-order type parameters are unsound

Re: [scala] Structural refinements containing methods with higher-order type parameters are unsound

by Meredith Gregory :: Rate this Message:

Reply to Author | View in Thread

Vladimir,

You are a monster! ;-) This is all great stuff! Scala is vastly improved by asking the kind of questions you're asking. And you've provided enough brain-candy to last me for a month.

Best wishes,

--greg

On Thu, Jun 18, 2009 at 5:01 AM, Vladimir Reshetnikov <v.reshetnikov@...> wrote:
Hi,

Consider the following definitions:

//////////////////////////////////////////////////////////////////////////
class C[T](var y : T)

object A {
 val a : AnyRef { def f[T[_]](x : T[int]) : T[Any] } = A
 def f[T[+_]](x : T[Int]) : T[Any] = x
}

//////////////////////////////////////////////////////////////////////////

Scala 2.8.0.r18050-b20090618020144 compiler compiles this without
errors. But it seems unsound, as we can write:

scala> val c = new C(1)
c: C[Int] = C@9e7d46

scala> val c1 = A.a.f[C](c)
c1: C[Any] = C@9e7d46

scala> c1.y = ""

scala> c.y
java.lang.ClassCastException: java.lang.String cannot be cast to
java.lang.Integer
       at scala.runtime.BoxesRunTime.unboxToInt(Unknown Source)
       at .<init>(<console>:7)
       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.Na...


Is it a bug?
I also cannot find in SLS exact rules, which determine what values can
be assigned to a variable of a structural type.

Thanks,
Vladimir



--
L.G. Meredith
Managing Partner
Biosimilarity LLC
1219 NW 83rd St
Seattle, WA 98117

+1 206.650.3740

http://biosimilarity.blogspot.com

 « Return to Thread: [scala] Structural refinements containing methods with higher-order type parameters are unsound