Match type widens the resulting type
See original GitHub issueCompiler 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.
Issue Analytics
- State:
- Created a year ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Pattern matching and type safety in TypeScript - LogRocket Blog
We can now simulate pattern matching with a switch statement. The function allows us to match on a tag of a value of...
Read more >Documentation - Type Compatibility - TypeScript
Type compatibility in TypeScript is based on structural subtyping. Structural typing is a way of relating types based solely on their members.
Read more >Match Types - Scala 3 - EPFL
Match Types. A match type reduces to one of its right-hand sides, depending on the type of its scrutinee. For example: type Elem[X]...
Read more >Way to tell TypeScript compiler Array.prototype.filter removes ...
Type 'undefined' is not assignable to type 'number'. I can cast the resulted array to number[] like below knowing filter function removes ...
Read more >Demystifying TypeScript's Extract Type - Widen Engineering
Since string and number are not assignable to Function , they will not be included in the resulting type. This results in FunctionArgs ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
There are many tickets about enums, unions, widening, and inference as an intersection of features.
It would be nice if there were a FAQ page summarizing the common questions. That page could also clarify what is “established behavior” and what might be “avenues of research”. In particular, it could list the use cases and the idioms.
The other new bit is that enums are preferentially widened, as mentioned in the docs.
(I started listing things that could go in the FAQ, after reading some tickets, but that is tedious and dreary to write and to read.)
On this ticket, the “further minimization” seems uninteresting, so I can’t tell if the OP is about the speculative tickets on “more precise types”.
But I sympathize with the need for a summary view.
Tried to find where I’d seen this (while thinking about #16107) and it took me too long. So, for future reference:
https://github.com/lampepfl/dotty/blob/e587a813d45a237a46dcee56c94ffd93cd4adf77/compiler/src/dotty/tools/dotc/core/ConstraintHandling.scala#L674-L675