-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Match typing with trait types doesn't work #8493
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
That's working as intended. Reducing def res[X](x: X): Res[X] = x match {
case _: Foo => String
case _: Bar => Int
} However, if class Boom extends Foo with Bar
val i: Int = res[Boom](new Boom())
println(i + 1) //java.lang.ClassCastException: class String cannot be cast to Integer If |
@OlivierBlanvillain Hmm, makes sense, thanks. This means I can't ever expect matching on function types to be useful, right, since scala> type Res[F] = F match {
| case ((a, b) => t) => String
| case ( a => t) => Int
| }
scala> 0: Res[Int => Int]
1 |0: Res[Int => Int]
|^
|Found: (0 : Int)
|Required: Res[Int => Int] |
Yes, matching on fonction types isn't going to lead to anywhere. In your example I don't think functions being traits is the problem, instead is has to do with variance. I know it's currently really hard to understand why certain match types don't reduce... I plan to invest time into better error messages for match types, in the worse case something à la |
@OlivierBlanvillain Just to clarify for the record, I don't need to distinguish |
minimized code
Compilation output
expectation
It works as expected if you change the traits to classes, or if you reverse the order of the cases.
The text was updated successfully, but these errors were encountered: