Uploaded image for project: 'Scala Programming Language'
  1. Scala Programming Language
  2. SI-2066

Unsoundness in overriding methods with higher-order type parameters

    Details

      Description

      Try to compile the following code with Scala 2.7.5 final:

      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 is compiled without any errors. But then you can write:

      val p = new P(1)
      val palias = (new B():A).f[P](p)
      palias.y = "hello"
      

      Result:

      java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
      

      So, this way of overriding is unsound.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                retronym Jason Zaugg
                Reporter:
                nikov Vladimir Reshetnikov
                TracCC:
                Ismael Juma, Mark Harrah, Simon Ochsenreither
              • Votes:
                2 Vote for this issue
                Watchers:
                9 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: