- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Unsoundness in overriding methods with higher-order type parameters?
Tue, 2009-06-16, 14:28
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
Tue, 2009-06-16, 15:07
#2
Re: Unsoundness in overriding methods with higher-order type p
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@gmail.com> wrote:
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@gmail.com> 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@gmail.com> 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
Tue, 2009-06-16, 15:27
#3
Re: Unsoundness in overriding methods with higher-order type p
I use Scala 2.7.5 final.
Vladimir
On 6/16/09, James Iry 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 wrote:
>
>> Which version of Scala are you running? In both 2.7.5 and
>> 2.8.0.something
>> I get
>>
>> :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@gmail.com> 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
>>>
>>
>>
>
Wed, 2009-06-17, 12:57
#4
Re: Unsoundness in overriding methods with higher-order type p
Hi Vladimir,
You're right. That's a bug :-( Working on a fix now -- could you please file a ticket?
cheers adriaan
On Tue, Jun 16, 2009 at 4:04 PM, Vladimir Reshetnikov <v.reshetnikov@gmail.com> wrote:
You're right. That's a bug :-( Working on a fix now -- could you please file a ticket?
cheers adriaan
On Tue, Jun 16, 2009 at 4:04 PM, Vladimir Reshetnikov <v.reshetnikov@gmail.com> wrote:
I use Scala 2.7.5 final.
Vladimir
On 6/16/09, James Iry <jamesiry@gmail.com> 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@gmail.com> 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@gmail.com> 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
>>>
>>
>>
>
Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Wed, 2009-06-17, 15:27
#5
Re: Unsoundness in overriding methods with higher-order type p
Hi Adriaan,
Ticket #2066. Do not know your nickname in trac, so I did not reassigned it.
Vladimir
On 6/17/09, Adriaan Moors wrote:
> Hi Vladimir,
> You're right. That's a bug :-(
> Working on a fix now -- could you please file a ticket?
>
> cheers
> adriaan
>
> On Tue, Jun 16, 2009 at 4:04 PM, Vladimir Reshetnikov <
> v.reshetnikov@gmail.com> wrote:
>
>> I use Scala 2.7.5 final.
>>
>> Vladimir
>>
>> On 6/16/09, James Iry 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 wrote:
>> >
>> >> Which version of Scala are you running? In both 2.7.5 and
>> >> 2.8.0.something
>> >> I get
>> >>
>> >> :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@gmail.com> 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
>> >>>
>> >>
>> >>
>> >
>>
>>
>> Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
>>
>
Wed, 2009-06-17, 15:57
#6
Re: Unsoundness in overriding methods with higher-order type p
Thanks! I reassigned it. (it's "moors" ;-))
adriaan
On Wed, Jun 17, 2009 at 4:19 PM, Vladimir Reshetnikov <v.reshetnikov@gmail.com> wrote:
adriaan
On Wed, Jun 17, 2009 at 4:19 PM, Vladimir Reshetnikov <v.reshetnikov@gmail.com> wrote:
Hi Adriaan,
Ticket #2066. Do not know your nickname in trac, so I did not reassigned it.
Vladimir
On 6/17/09, Adriaan Moors <adriaan.moors@cs.kuleuven.be> wrote:
> Hi Vladimir,
> You're right. That's a bug :-(
> Working on a fix now -- could you please file a ticket?
>
> cheers
> adriaan
>
> On Tue, Jun 16, 2009 at 4:04 PM, Vladimir Reshetnikov <
> v.reshetnikov@gmail.com> wrote:
>
>> I use Scala 2.7.5 final.
>>
>> Vladimir
>>
>> On 6/16/09, James Iry <jamesiry@gmail.com> 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@gmail.com> 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@gmail.com> 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
>> >>>
>> >>
>> >>
>> >
>>
>>
>> Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
>>
>
Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
<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@gmail.com> wrote: