Cannot override a type with non-volatile upper bound

后端 未结 3 1777
一个人的身影
一个人的身影 2020-12-31 13:30


I have a compiler error in scala and I don\'t know what does it refer to:
Assume these declarations:

trait Abstract {
  type MyType
}
trait AInner
trait         


        
3条回答
  •  囚心锁ツ
    2020-12-31 14:10

    What is wrong with this?

    trait B extends Abstract with A {
      override type MyType <: BInner with AInner
    }
    

    In any realization of trait B, MyType will always be the same type as seen from trait A, so upper-bounding it by itself does not make any sense.

    If in the upper piece of code, it bothers you that you'll have to rewrite trait B if you change the bound in trait A, use:

    trait A extends Abstract{
      type ABound = AInner
      type MyType <: AInner
    }
    trait B extends Abstract with A {
      override type MyType <: BInner with ABound
    }
    

提交回复
热议问题