Scala volatile types: how is @uncheckedStable unsafe?

I know that volatiles in Scala exist for modeling

the possibility that the type or type instance of a type has no nonzero value

( http://www.scala-lang.org/files/archive/spec/2.11/03-types.html#volatile-types )

But what is the problem with this? Is there an example that uses @uncheckedStable (see http://www.scala-lang.org/files/archive/spec/2.11/11-annotations.html#scala-compiler-annotations ) that creates unsafe code?

+5
source share
1 answer
 object Main extends App { trait A { type T = Int } trait B { type T <: String } def f(b: B)(t: bT) = t.length @annotation.unchecked.uncheckedStable val x: A with B = null val y: xT = 0 // legal because x is A f(x)(y) } Now running... [info] Running Main java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String 

(Based on retronym's answer on Unable to override a type with an unstable upper bound .)

+1
source

All Articles