我在Scala中有一個通用的F-bound特徵。讓我編寫方法返回相同的底層實現類型,超!但是現在讓我們說一個子特質也定義了需要F-邊界的方法。 Scala是送我回來的編譯錯誤是沒有意義:Scala - 繼承樹中的多個F-bound類型
package sandbox
import sandbox.ComplexImpl.AnyComplexImpl
import scala.language.existentials
trait FBounded[IMPL <: FBounded[IMPL]] { self: IMPL =>
def foo: IMPL
}
trait FBoundedUser[F <: FBounded[F]] {
def bar(value: F): F = value.foo
}
trait SimpleImpl extends FBounded[SimpleImpl] {
override def foo: SimpleImpl = this
}
object SimpleUser extends FBoundedUser[SimpleImpl]
// A-OK so far...
trait ComplexImpl[IMPL <: ComplexImpl[IMPL]] extends FBounded[IMPL] { self: IMPL =>
def baz: IMPL
}
object ComplexImpl {
type AnyComplexImpl = ComplexImpl[T] forSome { type T <: ComplexImpl[T] }
}
object ComplexUser1 extends FBoundedUser[ComplexImpl[_]]
object ComplexUser2 extends FBoundedUser[AnyComplexImpl]
試圖用任何的ComplexUser1
或ComplexUser2
導致編譯:
Error:(32, 29) type arguments [sandbox.ComplexImpl.AnyComplexImpl] do not conform to trait
FBoundedUser's type parameter bounds [F <: sandbox.FBounded[F]]
這是沒有意義的我。 AnyComplexImpl
絕對實現了FBounded
。我錯過了什麼,或者是類型檢查器在這裏讓我失望?
編輯:
class Concrete() extends ComplexImpl[Concrete] {
override def baz: Concrete = this
override def foo: Concrete = this
}
object ComplexUser3 extends FBoundedUser[Concrete]
編譯就好了。那麼爲什麼不能使用通用版本呢?
啊哈! Co/contravariance是類型系統的一個角落,我仍然試圖更好地把握。謝謝。 –