gpt4 book ai didi

Scala volatile 类型 : How is @uncheckedStable unsafe?

转载 作者:行者123 更新时间:2023-12-04 21:35:59 29 4
gpt4 key购买 nike

我知道 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 )

但这究竟有什么问题呢?是否有使用 @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

(基于retronym 对 Cannot override a type with non-volatile upper bound 的回答。)

关于Scala volatile 类型 : How is @uncheckedStable unsafe?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38003468/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com