Scala Programming Language
  1. Scala Programming Language
  2. SI-6575

Broken type inference with stackable traits

    Details

      Description

      (From at https://groups.google.com/d/topic/scala-user/2pcH-ck46Qg/discussion)

      with Scala 2.10.0-RC1 I found an issue with stackable traits that
      doesn't come up with Scala 2.9.2. Based on trait X

      trait X { def foo: PartialFunction[Int, Int] } 
      

      and two stackable traits Y and Z (that extend X):

      trait Y extends X { abstract override def foo = { case i => super.foo(i) * 2 } } 
      trait Z extends X { abstract override def foo = { case i => super.foo(i) + 3 } }
      

      I have another stackable trait Comb that combines what Y and Z are doing
      (and that does some additional work):

      trait Comb extends Y with Z { abstract override def foo: 
      PartialFunction[Int, Int] = { case i => super.foo(i) - 2 } } 
      

      This compiles with Scala 2.9.2 without problems but with Scala
      2.10.0-RC1 I'm getting the following error:

      [error] ...: missing parameter type for expanded function 
      [error] The argument types of an anonymous function must be fully known. 
      (SLS 8.5) 
      [error] Expected type was: 
      scala.runtime.AbstractPartialFunction[Int,Int] with Serializable 
      [error]   trait Comb extends Y with Z { abstract override def foo = { 
      case i => super.foo(i) - 2 } } 
      [error] ^ 
      [error] one error found 
      

      Of course I can get around this issue by writing the foo method type
      explicitly

      trait Y extends X { abstract override def foo: PartialFunction[Int, Int] 
      = { case i => super.foo(i) * 2 } } 
      trait Z extends X { abstract override def foo: PartialFunction[Int, Int] 
      = { case i => super.foo(i) + 3 } } 
      trait Comb extends Y with Z { abstract override def foo: 
      PartialFunction[Int, Int] = { case i => super.foo(i) - 2 } } 
      

      but then any concrete class that should be modified with Comb must add
      the foo method type as well i.e.

      class Temp extends X { def foo: PartialFunction[Int, Int] = { case i => 
      i } } 
      val tmp = new Temp with Comb 
      // ... 
      

      which wasn't necessary with Scala 2.9.2 either. If the type is omitted i.e.

      class Temp extends X { def foo = { case i => i } } 
      val tmp = new Temp with Comb 
      // ... 
      

      I'm getting

      [error] ...: overriding method foo in class Temp of type => 
      scala.runtime.AbstractPartialFunction[Int,Int] with Serializable; 
      [error]  method foo in trait Comb of type => PartialFunction[Int,Int] 
      has incompatible type 
      [error]   val tmp2 = new Temp with Comb 
      [error]                  ^ 
      [error] one error found 
      

        Activity

        Show
        Jason Zaugg added a comment - https://github.com/scala/scala/pull/1528
        Hide
        Josh Suereth added a comment -

        This appears to have no workaround.

        Show
        Josh Suereth added a comment - This appears to have no workaround.
        Show
        Adriaan Moors added a comment - https://github.com/scala/scala/pull/1530

          People

          • Assignee:
            Jason Zaugg
            Reporter:
            Martin Krasser
          • Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development