Pattern matching against type-member with Aux-pattern

泄露秘密 提交于 2020-12-31 06:04:59

问题


Consider:

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

The compileError function fails to compile with the following error:

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")

I don't really understand the incompatibility error. All B, C and D are instances of A, what's wrong with it and why the compilesOk function compiles fine?

来源:https://stackoverflow.com/questions/65176310/pattern-matching-against-type-member-with-aux-pattern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!