I was trying to answer this question, as I thought I knew the answer. Turns out, I did not know quite enough :/
Here is a test I have done:
class Inst[T] {
def is(x: Any) = scala.util.Try { as(x) }.isSuccess
def as(x: Any): T = x.asInstanceOf[T]
}
scala> new Inst[String].is(3)
res17: Boolean = true
scala> new Inst[String].as(3)
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
... 33 elided
What is going on here? Why does only the second call to as
throw, but not the first one?