-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Variance does not play well with pattern matching #22993
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
final class Bar[+T] extends Foo[T] Observing that this compiles. Both |
Well, a minimized example does not capture the real use case. 🤷♂ In reality, I need |
See https://contributors.scala-lang.org/t/avoid-overly-conservative-warnings-about-type-testability-in-exhaustive-matches/7083 . Allowing to return Admittedly |
@sjrd Then making Here's an updated snippet with |
With covariance you can write |
That works as a workaround, thanks. (UPDATE: not that great actually—see my next comment.) In general, I avoid using patterns like that (i.e. using a specific type like |
@sjrd I have just tried to apply the workaround in my original case and it triggers a "the type test [...] cannot be checked at runtime" warning: (I have changed lowercase The definitions are sealed trait Motif[+F[_], T]
sealed trait Object[+F[_], Ps] extends Motif[F, Obj[Ps]] |
@TomasMikula wait, does it?
|
@Gedochao You are right, it doesn't 🤦♂ I only tried 3.3.5 in Scastie, which does not show any error, so I assumed it compiled, but probably something in Scastie just crashes fatally without showing any error 🤷♂ |
Compiler version
3.6.4
(Works in 3.3.5)It does not, in fact, work in 3.3.5. Scastie just did not report any error.Minimized code
(snippet has been editted to make
Bar
covariant)https://scastie.scala-lang.org/EYEO0tDKSNqUJd4ypwRnKAhttps://scastie.scala-lang.org/emJhbNmrQd2WYQEdjBzwow
Output
Expectation
Should typecheck,
as it does with Scala 3.3.5.The text was updated successfully, but these errors were encountered: