我知道Scala中的volatile类型可用于建模
类型参数或类型的抽象类型实例不具有任何非空值
的可能性
(http://www.scala-lang.org/files/archive/spec/2.11/03-types.html#volatile-types)
但究竟是什么问题呢?是否有使用@uncheckedStable
(请参阅http://www.scala-lang.org/files/archive/spec/2.11/11-annotations.html#scala-compiler-annotations)产生不安全代码的示例?
答案 0 :(得分:1)
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
(基于反词对Cannot override a type with non-volatile upper bound的回答。)