Description
This should not compile; it should not be able to infer a T
when calling onestep
. We use that to throw a ClassCastException
.
package forsomescope2
trait Magic[S] {
def init: S
def step(s: S): String
}
object Main extends App {
def onestep[T](m: => Magic[T]) =
m.step(m.init)
def flip: () => Magic[_] = {
var z = false
() => {
z = !z
if (z) new Magic[Int] {
def init = 0
def step(s: Int) = (s - 1).toString
} else new Magic[String] {
def init = "hi"
def step(s: String) = s.reverse
}
}
}
def boom = {
val flipper = flip
onestep(flipper())
}
boom
}
(scastie here) throws
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
at scala.runtime.BoxesRunTime.unboxToInt(BoxesRunTime.java:101)
at forsomescope2.Main$$anon$1.step(ForsomeScope.scala:16)
at forsomescope2.Main$.onestep(ForsomeScope.scala:10)
at forsomescope2.Main$.boom(ForsomeScope.scala:28)
at forsomescope2.Main$.delayedEndpoint$forsomescope2$Main$1(ForsomeScope.scala:30)
at forsomescope2.Main$delayedInit$body.apply(ForsomeScope.scala:8)
This doesn't apply to the Magic
variant from #9410, except if you do the intermediate introduction of forSome
, which is already introduced here.