Scala volatile类型:@uncheckedStable如何不安全?
发布时间:2020-12-16 18:31:42 所属栏目:安全 来源:网络整理
导读:我知道 Scala中的volatile类型可以用来建模 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) 但究竟是什
我知道
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 (根据反义词对Cannot override a type with non-volatile upper bound的回答.) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |