在 class 构造函数中继承抽象类型

Inherited abstract type in case class constructor

我正在将一些参数化的 F-Bounded 类型转换为抽象类型 classes。原来的 classes 是:

sealed trait A[AA <: A[AA]] {
  self =>
  val data: String
}
case class AInst(data: String) extends A[AInst]

sealed trait B[BB <: B[BB, AA], AA <: A[AA]] {
  self: BB =>
  val content: AA
}
case class BInst[AA <: A[AA]](content: AA) extends B[BInst[AA], AA]

我想要两个相同的案例 classes,但特征应该失去所有参数。这是我的尝试:

sealed trait A2 {
  self =>
  type AA <: A2 {type AA = self.AA}
  val data: String
}

case class A2Inst(data: String) extends A2

sealed trait B2 {
  self =>
  type BB <: A2 {type BB = self.BB}
  type AA <: A2 {type AA = self.AA}
  val content: AA
  }

case class B2Inst[AHere <: A2 {type AA = AHere}](content: AHere) extends B2 {
  self =>
  type AA = AHere
}

val a2 = A2Inst("A2")
val b2 = B2Inst(a2)

不幸的是B2Inst 无法编译。 class 案例的正确定义是什么?

您在 A2Inst 的定义中缺少类型细化。

case class A2Inst(data: String) extends A2 { type AA = A2Inst }

总计:

sealed trait A2 {
  self =>
  type AA <: A2 {type AA = self.AA}
  val data: String
}
case class A2Inst(data: String) extends A2 { type AA = A2Inst }

sealed trait B2 {
  self =>
  type BB <: A2 {type BB = self.BB}
  type AA <: A2 {type AA = self.AA}
  val content: AA
}

case class B2Inst[AHere <: A2 {type AA = AHere}](content: AHere) extends B2 {
  self =>
  type AA = AHere
}

val a2 = A2Inst("A2")
val b2 = B2Inst(a2)

// Exiting paste mode, now interpreting.

defined trait A2
defined class A2Inst
defined trait B2
defined class B2Inst
a2: A2Inst = A2Inst(A2)
b2: B2Inst[A2Inst] = B2Inst(A2Inst(A2))