考虑:
sealed trait A
case object B extends A
case object C extends A
case object D extends A
sealed trait Test {
type AA <: A
val aa: AA
}
object Test {
type Aux[AAA <: A] = Test { type AA = AAA }
}
def compilesOk(t: Test) = t.aa match {
case _: B.type =>
println("B")
case _: C.type =>
println("C")
case _: D.type =>
println("D")
}
def compileError(t: Test) = t.aa match {
case B => println("B")
case C => println("C")
case D => println("D")
}
该compileError
函数无法编译并出现以下错误:
Error:(47, 10) pattern type is incompatible with expected type;
found : B.type
required: t.AA
case B => println("B")
Error:(48, 10) pattern type is incompatible with expected type;
found : C.type
required: t.AA
case C => println("C")
Error:(49, 10) pattern type is incompatible with expected type;
found : D.type
required: t.AA
case D => println("D")
我真的不明白不兼容错误。所有B
,C
并且D
是 的实例A
,它有什么问题以及为什么compilesOk
函数编译得很好?