0

Any thoughts why approach 2 is not supported, or am I missing any syntax ?

trait Bar { }
class BarImpl extends Bar{ }

1 Scala allows to override with generic type parameter

abstract class Foo {
  type T <: Bar
  def bar1(f: T): Boolean
}

class FooImpl extends Foo {
  type T = BarImpl
  override def bar1(f: BarImpl): Boolean = true 
}

2 While it doesn't allow with generic type class

abstract class Foo2[T <: Bar] {
  def bar1(f: T): Boolean
}

class FooImpl2[BarImpl] extends Foo2 {
  // Error: Method bar1 overrides nothing
  override def bar1(f: BarImpl): Boolean = true
}
skjagini
  • 2,672
  • 4
  • 29
  • 50

1 Answers1

3

In your implementation of FooImpl2 you are passing BarImpl as a new Type parameter for FooImpl2 instead of passing it to Foo2 (that is the one that is expecting a type parameter).

So what you have to do is:

class FooImpl2 extends Foo2[BarImpl] {
    override def bar1(f: BarImpl): Boolean = true
}