Closed
Description
Compiler version
3.1.0
Minimized code
type A[X] = X match
case Int => Int
case _ => B[X]
def a[X](x: X): A[X] = x match
case v: Int => v
case _ => b(x)
type B[X] = X match
case String => String
def b[X](x: X): B[X] = x match
case v: String => v
Output
-- [E007] Type Mismatch Error: foo.scala:6:17 ----------------------------------
6 | case v: Int => v
| ^
| Found: (v : X & Int)
| Required: A[X]
|
| Note: a match type could not be fully reduced:
|
| trying to reduce A[X]
| failed since selector X
| does not match case Int => Int
| and cannot be shown to be disjoint from it either.
| Therefore, reduction cannot advance to the remaining case
|
| case _ => B[X]
longer explanation available when compiling with `-explain`
-- [E007] Type Mismatch Error: foo.scala:7:18 ----------------------------------
7 | case _ => b(x)
| ^^^^
| Found: B[X]
| Required: A[X]
|
| Note: a match type could not be fully reduced:
|
| trying to reduce B[X]
| failed since selector X
| does not match case String => String
| and cannot be shown to be disjoint from it either.
longer explanation available when compiling with `-explain`
2 errors found
Expectation
Should compile?
The type calculation works fine for chained Match Types. Only when adding the value level implementation it fails.
This works:
type A[X] = X match
case Int => Int
case _ => B[X]
def a[X](x: X): A[X] = (x match
case v: Int => v
case _ => b(x)
).asInstanceOf[A[X]]
type B[X] = X match
case String => String
def b[X](x: X): B[X] = x match
case v: String => v