我知道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)生成不安全代码的示例?
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
Run Code Online (Sandbox Code Playgroud)
(基于 Retronym 对Cannot override a type with non-volatile upper limit 的回答。)
| 归档时间: |
|
| 查看次数: |
197 次 |
| 最近记录: |