How can I override a method with a dependent return type?

I'm having problems in Scala 2.9.2, which implements a method that declares a dependent return type. Following code

object DependentTypesQuestion {
  def ??? = throw new UnsupportedOperationException
  trait X {
    trait Y
  }
  trait Z {
    def z(x: X): x.Y
  }
  object Z extends Z {
    override def z(x: X): x.Y = ???
  }
}

in section 2.9.2 displays the following error message at compile time:

overriding method z in trait Z of type (x: DependentTypesQuestion.X)x.Y;  method z has incompatible type

In 2.10.0-M4, the problem seems to be fixed, but unfortunately my project is now tied to 2.9.

Is there a way around this problem in 2.9.2?

(Alternatively, is there any perspective in 2.9.3 that includes a fix with feedback from 2.10?)

+5
source share
1 answer

If you are really stuck with 2.9.x, then the following may be used for you:

object DependentTypesQuestion {
  def ??? = throw new UnsupportedOperationException
  trait X {
    trait Y
  }
  trait Z[D[_ <: X with Singleton]] {
    def z[T <: X with Singleton](x: T): D[T]
  }

  type Dep[T <: X with Singleton] = T#Y

  object Z extends Z[Dep] {
    override def z[T <: X with Singleton](x: T): x.Y = ???
  }
}
+3
source

All Articles