首先錯誤:在簽名混淆Scala的類型不匹配錯誤與「uncheckedVariance」
/Users/rob/Workspace/Boiled.scala:9: error: type mismatch;
found : DataSetup{type Mem <: Product with Serializable{val ids: List[Int]; def copy(ids: List[Int]): this.Mem; def copy$default$1: List[Int]}; object Mem; type Memory = this.Mem}
required: DataSetup{type Mem <: Product with Serializable{val ids: List[Int]; def copy(ids: List[Int]): this.Mem; def copy$default$1: List[Int] @scala.annotation.unchecked.uncheckedVariance}; object Mem; type Memory = this.Mem}
val dataSetup = new DataSetup {
^
可愛,不是嗎?它指向一條線,我嘗試創建一個DataSetup
特徵的實例。它當然是真實代碼的簡化版本。
trait DataSetup {
type Memory <: AnyRef with Serializable
def run(): Memory
}
object Use {
val dataSetup = new DataSetup { // <---- error reported here
case class Mem(ids: List[Int])
type Memory = Mem
def run(): Memory = {
val ids = List(1,2,3)
Mem(ids)
}
}
}
我真的不知道它在抱怨什麼。任何人?
根據記錄,它的工作原理,如果你只是命名案例類的內存。一個子類可以「實現」一個具有類或特徵的抽象類型。 – nafg
另一個工作變體:將'val dataSetup = new'更改爲'object dataSetup extends' – nafg
還有一個:'val dataSetup:DataSetup = ...'。有趣的是,在2.9的錯誤消息的措辭略有不同:'發現:Use.dataSetup.type(與基礎型...' – nafg