You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@main deftest:Unit= {
classC { typeT1; typeT2 }
defpmatch(s: C): s.T2= s match {
casep: (C { typeT1=Int; typeT2>:T1 } & s.type) =>
(3: p.T1): p.T2casep: (C { typeT1=String; typeT2>:T1 } & s.type) =>
("this branch should be matched": p.T1): p.T2
}
// ClassCastException: class java.lang.Integer cannot be cast to class java.lang.Stringvalx= pmatch(newC { typeT1=String; typeT2=String })
}
Output
Click to expand
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
at main$package$.test(main.scala:12)
at test.main(main.scala:1)
at sun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sbt.Run.invokeMain(Run.scala:115)
at sbt.Run.execute$1(Run.scala:79)
at sbt.Run.$anonfun$runWithLoader$4(Run.scala:92)
at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:23)
at sbt.util.InterfaceUtil$$anon$1.get(InterfaceUtil.scala:10)
at sbt.TrapExit$App.run(TrapExit.scala:257)
at java.lang.Thread.run(Thread.java:748)
Expectation
We could expect the second branch to be matched, but due to erasure, I do not think it is possible.
I believe issuing a warning specifying that such a runtime check cannot be performed could be a consideration. I also think that (3: p.T1): p.T2 should be rejected since we cannot be certain that we have { type T1 = Int; type T2 >: T1 } (similarly for the second match branch).
The text was updated successfully, but these errors were encountered:
It seems that something similar happens for type refinement:
traitS[A]
traitP[A] extendsS[A]
defpmatch[A](s: S[A]):A= s match {
casep: P[_ >:Int] =>10
}
// ClassCastException: class java.lang.Integer cannot be cast to class java.lang.Booleanvalx= pmatch(newP[Boolean]{})
Minimized code
Output
Click to expand
Expectation
We could expect the second branch to be matched, but due to erasure, I do not think it is possible.
I believe issuing a warning specifying that such a runtime check cannot be performed could be a consideration. I also think that
(3: p.T1): p.T2
should be rejected since we cannot be certain that we have{ type T1 = Int; type T2 >: T1 }
(similarly for the second match branch).The text was updated successfully, but these errors were encountered: