在編譯器中刪除此檢查可讓我們發現潛在的不穩定性。
diff --git a/src/compiler/scala/tools/nsc/typechecker/Typers.scala b/src/compiler/scala/tools/nsc/typechecker/Typers.scala
index 37a7e3c..78a8959 100644
--- a/src/compiler/scala/tools/nsc/typechecker/Typers.scala
+++ b/src/compiler/scala/tools/nsc/typechecker/Typers.scala
@@ -5128,8 +5128,7 @@ trait Typers extends Adaptations with Tags {
def typedSelectFromTypeTree(tree: SelectFromTypeTree) = {
val qual1 = typedType(tree.qualifier, mode)
- if (qual1.tpe.isVolatile) TypeSelectionFromVolatileTypeError(tree, qual1)
- else typedSelect(tree, qual1, tree.name)
+ typedSelect(tree, qual1, tree.name)
}
def typedTypeBoundsTree(tree: TypeBoundsTree) = {
然後,從編譯器測試用例非法選型揮發性類型運行代碼:
scala> class A; class B extends A
defined class A
defined class B
scala> trait C {
| type U
| trait D { type T >: B <: A }
| val y: (D with U)#T = new B
| }
defined trait C
scala> class D extends C {
| trait E
| trait F { type T = E }
| type U = F
| def frob(arg : E) : E = arg
| frob(y)
| }
defined class D
scala> new D
java.lang.ClassCastException: B cannot be cast to D$E
據我瞭解,這個問題來自這樣一個事實Scala沒有真正交點類型。
scala> type A = { type T = Int }
defined type alias A
scala> type B = { type T = String }
defined type alias B
scala> "": (A with B)#T
res16: String = ""
scala> 0: (A with B)#T
<console>:37: error: type mismatch;
found : Int(0)
required: String
0: (A with B)#T
^
這可能在未來改變,如果研究Dependent Object Types (DOT)掛果。
爲什麼在非易失性類型中禁止抽象方法成員? – Blaisorblade 2014-07-01 23:03:45