我有一些无法编译的 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
$ 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
这个是最不解的)
不幸的是,在我的用例中,我无法更改其中的任何一个(在这个小示例中,原因尚不清楚,但实际代码依赖于它们)。我是在做被禁止的事情,还是这是编译器的限制?