[英]Scala this.type conformance to type parameter bounds of supertype
我正在嘗試編譯以下代碼:
class MyClass {
def foo(): this.type = Helper.bar(this)
}
class ChildClass extends MyClass
object Helper {
def bar[A <: MyClass](cls: A): A = cls
}
產生的編譯器錯誤是:
type arguments [MyClass.this.type] do not conform to method bar's type parameter bounds [A <: MyClass]
使用上面提供的方法簽名,我可以做些什么來使這個compile屬性? 看來MyClass.this.type應該是有效的類,並且我不想在調用Helper時強制轉換兩次(一次進出一次)。
從不推斷單例類型。 你得寫
Helper.bar[this.type](this)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.