将Scala类型不匹配错误与签名中的“uncheckedVariance”混淆

时间:2013-12-08 22:54:42

标签: scala scala-2.10 type-mismatch

首先是错误:

/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)
    }
  }

}

我真的不知道它在抱怨什么。任何人吗?

1 个答案:

答案 0 :(得分:2)

有时,最新的2.11里程碑中的错误消息已得到改善。

<console>:14: error: type mismatch;
 found   : DataSetup{type Mem(in <refinement of DataSetup>)(in <refinement of DataSetup>)(in <refinement of DataSetup>)(in <refinement of DataSetup>) <: Product with Serializable{val ids: List[Int]; def copy(ids: List[Int]): this.Mem(in <refinement of DataSetup>)(in <refinement of DataSetup>)(in <refinement of DataSetup>)(in <refinement of DataSetup>); def copy$default$1: List[Int]}; type Memory = this.Mem(in <refinement of DataSetup>)(in <refinement of DataSetup>)(in <refinement of DataSetup>)(in <refinement of DataSetup>)}
 required: DataSetup{type Mem(in <refinement of DataSetup>)(in <refinement of DataSetup>)(in <refinement of DataSetup>)(in <refinement of DataSetup>) <: Product with Serializable{val ids: List[Int]; def copy(ids: List[Int]): this.Mem(in <refinement of DataSetup>)(in <refinement of DataSetup>)(in <refinement of DataSetup>)(in <refinement of DataSetup>); def copy$default$1: List[Int] @scala.annotation.unchecked.uncheckedVariance}; object Mem; type Memory = this.Mem(in <refinement of DataSetup>)(in <refinement of DataSetup>)(in <refinement of DataSetup>)(in <refinement of DataSetup>)}
         val dataSetup = new DataSetup {     // <---- error reported here
             ^

无论如何,看起来注释(合成地添加到copy的默认arg方法)在推断类型中丢失。

val dataSetup: DataSetup = new DataSetup {     // <---- no error here

显然,没有注释,类型不符合。值得问为什么:

https://issues.scala-lang.org/browse/SI-8071

与另一个经典的paulpism:

  

我在某些时候失去了数量,但我没有注意到许多改进   AnyRef。