简体   繁体   中英

Why do Type Member bounds stop working when self types are involved

Say I define some traits with a self-type. Both the traits and the self type have a abstract type member. The abstract type member in the self-type should be overridden by the self-type in the trait.

trait Foo{
  type My
  def make:Seq[My]
}

trait Component {
  type My
}

trait Bar extends Foo { this:Component =>
  override type My <: StuffDoer

  def len = make.map(_.doStuff)

  class StuffDoer(str:String) {
    def doStuff = "blah"
  }
}

This doesn't work and gives the error:

Error:(20, 24) value doStuff is not a member of Bar.this.My
    def len = make.map(_.doStuff)

It seems that My inside of Bar is not necessarily a StuffDoer type but why? What are the exact bounds on My inside of Bar ? Does Component override it's type boundaries?

What is even more strange is when I change override type My <: StuffDoer inside of Bar to:

override type My = StuffDoer

then suddenly everything compiles. How come???

If class StuffDoer has method doStuff then all subclasses do as well but not all subtypes do. For example Nothing and Null are subtypes of StuffDoer but you can't call x.doStuff on variable x of type Nothing or Null .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM