Regression from Scala 2: Type variable in pattern match works with invariant type, fails with covariant
See original GitHub issueScala 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:
- Created 2 years ago
- Comments:6 (6 by maintainers)
Top 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 >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
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.
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.