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.

Regression from Scala 2: Type variable in pattern match works with invariant type, fails with covariant

See original GitHub issue

Scala 2.13.8 accepts both of these:

scala> def foo[T](xs: Set[T]): T = xs match { case xs: Set[u] => xs.head: u }
def foo[T](xs: Set[T]): T

scala> def foo[T](xs: List[T]): T = xs match { case xs: List[u] => xs.head: u }
def foo[T](xs: List[T]): T

Scala 3 accepts the Set one, but fails on the List version. with -explain for verbosity,

scala> def foo[T](xs: List[T]): T = xs match { case xs: List[u] => xs.head: u }
-- [E007] Type Mismatch Error: -------------------------------------------------
1 |def foo[T](xs: List[T]): T = xs match { case xs: List[u] => xs.head: u }
  |                                                            ^^^^^^^^^^
  |                                                            Found:    u
  |                                                            Required: T
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  |
  | Tree: xs.head:u
  | I tried to show that
  |   u
  | conforms to
  |   T
  | but the comparison trace ended with `false`:
  |
  |   ==> u  <:  T
  |     ==> u  <:  Nothing in frozen constraint
  |       ==> Any  <:  Nothing (left is approximated) in frozen constraint
  |       <== Any  <:  Nothing (left is approximated) in frozen constraint = false
  |       ==> Any  <:  Nothing in frozen constraint
  |       <== Any  <:  Nothing in frozen constraint = false
  |     <== u  <:  Nothing in frozen constraint = false
  |     ==> Any  <:  T (left is approximated)
  |       ==> Any  <:  Nothing in frozen constraint
  |       <== Any  <:  Nothing in frozen constraint = false
  |     <== Any  <:  T (left is approximated) = false
  |     ==> Any  <:  T in frozen constraint
  |       ==> Any  <:  Nothing in frozen constraint
  |       <== Any  <:  Nothing in frozen constraint = false
  |     <== Any  <:  T in frozen constraint = false
  |   <== u  <:  T = false
  |
  | The tests were made under the empty constraint
   -----------------------------------------------------------------------------

I assume that the difference between Set and List here is that Set is invariant, List covariant.

(fyi @dwijnand; this came up during one of our pairing sessions)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
anatoliykmetyukcommented, Apr 4, 2022

This issue was picked for the Issue Spree 14 of 12th April which takes place in a week from now. @dwijnand and @dos65 will be working on it. If you have any insight into the issue or guidance on how to fix it, please leave it here.

0reactions
anatoliykmetyukcommented, Apr 25, 2022

This issue was picked for the Issue Spree 15 of May 3rd which takes place a week from now. @dwijnand and @joke1196 will be working on it. If you have any insight into the issue or guidance on how to fix it, please leave it here.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Treating a covariant type as invariant in Scala? - Stack Overflow
I have a scenario where I'm trying to pattern-match against a case-class in a way that allows type inference to infer the type...
Read more >
The singleton instance trick used by =:= is unsound #8430
The real issue here is not pattern matching, it is not GADT, it is not singleton types. The problem is the cast. As...
Read more >
Variances | Tour of Scala
Scala supports variance annotations of type parameters of generic classes, to allow them to be covariant, contravariant, or invariant if no annotations are ......
Read more >
Match Types - Scala 3 - EPFL
Match Types Variance​​ All type positions in a match type (scrutinee, patterns, bodies) are considered invariant.
Read more >
Scala examples 3
... Pattern matching */ val t = (3,5,2) // declare three new variables x1,x2,x3 ... they all have to be of // same...
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