I know that volatile types in Scala are there to model
the possibility that a type parameter or abstract type instance of a type does not have any non-null value
(http://www.scala-lang.org/files/archive/spec/2.11/03-types.html#volatile-types)
But what is the problem with this exactly? Is there an example which uses @uncheckedStable
(see http://www.scala-lang.org/files/archive/spec/2.11/11-annotations.html#scala-compiler-annotations) which produces unsafe code?
object Main extends App {
trait A { type T = Int }
trait B { type T <: String }
def f(b: B)(t: b.T) = t.length
@annotation.unchecked.uncheckedStable val x: A with B = null
val y: x.T = 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 to Cannot override a type with non-volatile upper bound.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With