[英]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? 似乎“
My
的Bar
里面”不一定是StuffDoer
类型,但为什么呢? What are the exact bounds on My
inside of Bar
? My
在Bar
内部的确切界限是什么? Does Component
override it's type boundaries? Component
是否覆盖其类型边界?
What is even more strange is when I change override type My <: StuffDoer
inside of Bar
to: 更奇怪的是,当我将
Bar
内的override type My <: StuffDoer
更改为:
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. 如果
StuffDoer
类具有doStuff
方法,则所有子类也都可以,但并非所有子类型都可以。 For example Nothing
and Null
are subtypes of StuffDoer
but you can't call x.doStuff
on variable x
of type Nothing
or Null
. 例如
Nothing
和Null
是StuffDoer
子类型,但是您不能在Nothing
或Null
类型的变量x
上调用x.doStuff
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.