【发布时间】:2016-07-25 14:49:52
【问题描述】:
我对这里发生的事情感到很困惑:
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 的类型的详细信息也在运行时可用...
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 警告。这是怎么回事?
【问题讨论】:
标签: scala type-erasure