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

View: New views
4 Messages — Rating Filter:   Alert me  

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

by nikov :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

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

by Paul Phillips-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Thu, Jun 18, 2009 at 11:35:29PM +0500, Vladimir Reshetnikov wrote:
> Looks like unsoundness.

It sure does.  File it with this one, which also relies on structural
types being overlax.

  http://www.scala-lang.org/node/1109

--
Paul Phillips      | Eschew mastication.
Analgesic          |
Empiricist         |
all hip pupils!    |----------* http://www.improving.org/paulp/ *----------

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

by Vlad Patryshev :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

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

by nikov :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I think that co/contravariance is very useful, but co/contravariant
positions of type parameters should be checked more strictly.

Vladimir

On 6/20/09, Vlad Patryshev <vpatryshev@...> wrote:

> 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
>