2016-06-23 1 views
5

나는 스칼라 휘발성 유형은 유형 매개 변수 또는 유형의 추상 형식의 인스턴스가 null 이외의 값스칼라 휘발성 유형 : @uncheckedStable은 어떻게 안전하지 않습니까?

(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 참조)을 사용하는 예제가 있습니까?

답변

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에 retronym의 답변에 따라)

+0

흠,하지만이에 "작업"심지어 @ annotation.unchecked.uncheckedStable없이 내가 제대로 사양을 읽고 있어요 경우 (HTTP를 보인다 : // www.scala-lang.org/files/archive/spec/2.11/03-types.html#volatile-types), B와 A는 휘발성이 아닙니다. –

+0

네, 맞습니다. 그리고 첫 번째 답변을 수정하려고하면 uncheckedStable이 전혀 작동하지 않는 것 같습니다. http://scastie.org/20577 –

+0

유형에 대한 이유에 응답하지 않아 검사를 취소합니다. 변동성으로 인해 혼란 스럽습니다. –

관련 문제