scala3
scala3 copied to clipboard
Match type widens the resulting type
Compiler version
3.2.0
Minimized code
enum A {
case AA
case AB
}
enum B {
case BA
case BB
case BC
}
type P[X <: A] = X match {
case A.AA.type => B.BA.type | B.BB.type
case A.AB.type => B.BC.type
}
def p[X <: A](x: X): P[X] = x match {
case x: A.AA.type => B.BA
case x: A.AB.type => B.BC
}
p[A.AA.type](A.AA) // widened, BA: B
p[A.AB.type](A.AB) // widened, BC: B
sealed trait C
object CA extends C
object CB extends C
object CC extends C
type P2[X <: A] = X match {
case A.AA.type => CA.type | CB.type
case A.AB.type => CC.type
}
def p2[X <: A](x: X): P2[X] = x match {
case x: A.AA.type => CA
case x: A.AB.type => CC
}
p2[A.AA.type](A.AA) // widened, CA: C
p2[A.AB.type](A.AB) // not widened, CC: CC
Output
Expectation
I expect them not to be widened.