我有一些无法编译的Scala代码,我将其简化为问题的本质。
class Inner[T] {
class Value
val values = IndexedSeq.empty[Value]
}
class Outer[T] {
def inner = new Inner[T]
}
object TestApp {
def main(args: Array[String]) {
val outer: Outer[_] = null
val values = outer.inner.values
values(0)
}
}
我正在使用 2.9.1.final(英语:2.9.1.final
)$ scalac test.scala
test.scala:14: error: ambiguous reference to overloaded definition,
both method apply in trait SeqLike of type ((idx: Int)Inner[_$1]#Value) forSome { type _$1 }
and method apply in trait Function1 of type ((v1: Int)Inner[_$1]#Value) forSome { type _$1; type _$1; type _$1 }
match argument types (Int)
values(0)
^
one error found
如果我执行以下任何操作,我能够使编译错误消失:
- 删除内部类(
IndexedSeq.empty[String]
而不是IndexedSeq.empty[Value]
) - 删除存在类型(
Outer[String]
而不是Outer[_]
) - 删除 IndexedSeq.apply (
values.head
而不是values(0)
) - 将
def inner
更改为val inner
(这是最令人费解的一个)
不幸的是,在我的用例中,我无法更改其中任何一个(在这个小示例中不清楚为什么,但实际代码依赖于它们)。我是在做一些被禁止的事情,还是这是编译器的限制?
可能是一个限制,因为它在2.10.0-M4
中似乎很好。
当然,它给出了一个NullPointer
,因为outer
设置为null
。