限制为实现类型的Scala特征方法

因此,我在特征实现中遇到了一些问题,我认为这是一个非常简单的情况,我希望有一些我遗漏的简单解决方案。我希望在特征上有一个方法,它可以接受作为参数(并且只以值的形式返回被调用的具体实现的类型)。具体地说:

trait Foo {
  type ConcreteFoo // what to put here?
  def combine(that:ConcreteFoo):ConcreteFoo
}

class FooImpl1 extends Foo {
  def combine(that:FooImpl1):FooImpl1 = {
    // implementation
  }
}

class FooImpl2 extends Foo {
  def combine(that:FooImpl2):FooImpl2 = {
    // implementation
  }
}

现在,我有一个关于实现类的type Self = FooImpl,但如果可能的话,我更希望有一些关于特征的东西来处理它。

转载请注明出处:http://www.tochigihk.com/article/20230526/1448739.html