[英]Why are the bounds of type parameters ignored when using existential types in Scala?
[英]Why do Type Member bounds stop working when self types are involved
說我用自我類型定義一些特征。 特征和自我類型都具有抽象類型成員。 自類型中的抽象類型成員應被特征中的自類型覆蓋。
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"
}
}
這不起作用,並給出錯誤:
Error:(20, 24) value doStuff is not a member of Bar.this.My
def len = make.map(_.doStuff)
似乎“ My
的Bar
里面”不一定是StuffDoer
類型,但為什么呢? My
在Bar
內部的確切界限是什么? Component
是否覆蓋其類型邊界?
更奇怪的是,當我將Bar
內的override type My <: StuffDoer
更改為:
override type My = StuffDoer
然后突然一切都編譯了。 怎么會???
如果StuffDoer
類具有doStuff
方法,則所有子類也都可以,但並非所有子類型都可以。 例如Nothing
和Null
是StuffDoer
子類型,但是您不能在Nothing
或Null
類型的變量x
上調用x.doStuff
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.