Pattern matching against type-member with Aux-pattern

80 Views Asked by At

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?

0

There are 0 best solutions below