question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Match type widens the resulting type

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
som-snyttcommented, Sep 24, 2022

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.

0reactions
dwijnandcommented, Nov 16, 2022

Basically singleton types, except for those on actual objects, get widened to their underlying type.

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found