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

}

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

最佳答案 有时,错误消息在最新的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
             ^

无论如何,在推断类型中看起来像注释(合成地添加到默认的arg复制方法).

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

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

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

与另一个经典的paulpism:

I lost count at some point but I didn’t notice that many refinements
of AnyRef.

点赞