我对这里发生的事情感到很困惑:
import scala.collection.immutable._
object Main extends App {
sealed trait Node
sealed trait Group
case class Sheet(
val splat: String,
val charname: String,
val children: ListMap[String, Node],
val params0: ListMap[String, Param], //params0 to separate sheet-general parameters
val note: Option[Note]
) extends Node with Group
case class Attributes(val name: String) extends Node with Group
case class Param(val name: String, val value: String) extends Node
case class Note(val note: String) extends Node
我有三个版本的替换函数 - 最后一个是我实际上正在尝试编写的版本,其他版本只是调试。
class SheetUpdater(s: Sheet) {
def replace1[T <: Group](g: T): Unit = {
s.children.head match {
case (_, _:Sheet) =>
case (_, _:Attributes) =>
}
}
}
这个版本没有发出警告,所以显然我可以访问的类型s.children
在运行时。
class SheetUpdater(s: Sheet) {
def replace2[T <: Group](g: T): Unit = {
g match {
case _:Sheet =>
case _:Attributes =>
}
}
}
这个版本也没有,所以显然细节g
的类型是also在运行时可用...
class SheetUpdater(s: Sheet) {
def replace3[T <: Group](g: T): Unit = {
s.children.head match {
case (_, _:T) => //!
case (_, _:Attributes) =>
}
}
}
...但即便如此,这最终还是让我陷入了可怕的境地Abstract type pattern T is unchecked since it is eliminated by erasure
警告。这里发生了什么?