« Return to Thread: [scala] Unsoundness in overriding methods with higher-order type parameters?

Re: [scala] Unsoundness in overriding methods with higher-order type parameters?

by nikov :: Rate this Message:

Reply to Author | View in Thread

I use Scala 2.7.5 final.

Vladimir

On 6/16/09, James Iry <jamesiry@...> wrote:

> Oops, missed the cast to an A.  Indeed that's a problem
>
> trait A {
>   def f[T[_]](x : T[Int]) : T[Any]
> }
>
> class B extends A {
>   def f[T[+_]](x : T[Int]) : T[Any] = x
> }
>
> class P[Y](var y : Y)
>
>
> val p = new P(1)
>
> val palias = (new B():A).f[P](p)
>
> palias.y = "hello"
>
> scala> p.y
> java.lang.ClassCastException: java.lang.String cannot be cast to
> java.lang.Integer
>
>
>
> On Tue, Jun 16, 2009 at 6:51 AM, James Iry <jamesiry@...> wrote:
>
>> Which version of Scala are you running?  In both 2.7.5 and
>> 2.8.0.something
>> I get
>>
>> <console>:7: error: kinds of the type arguments (P) do not conform to the
>> expected kinds of the type parameters (type T).
>> P's type parameters do not match type T's expected parameters: type Y is
>> invariant, but type _ is declared covariant
>>        val palias = new B().f[P](new P[Int](1))
>>
>>
>> On Tue, Jun 16, 2009 at 6:27 AM, Vladimir Reshetnikov <
>> v.reshetnikov@...> wrote:
>>
>>> Hi,
>>>
>>> Consider the following code:
>>>
>>> //////////////////////////////////////////////////
>>> trait A {
>>>  def f[T[_]](x : T[Int]) : T[Any]
>>> }
>>>
>>> class B extends A {
>>>  def f[T[+_]](x : T[Int]) : T[Any] = x
>>> }
>>>
>>> class P[Y](var y : Y)
>>> //////////////////////////////////////////////////
>>>
>>> It accepted by the Scala 2.7.5 compiler without any errors. But is
>>> seems unsound, because then we can write (new B:A).f[P](new
>>> P[Int](1)), and nonvariant P[Int] is coerced to P[Any].
>>>
>>> Thanks,
>>> Vladimir
>>>
>>
>>
>

 « Return to Thread: [scala] Unsoundness in overriding methods with higher-order type parameters?