I have some Scala code I cannot get to compile, and I simplified it down to what appears to be the essence of the problem.
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)
}
}
I am using 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
I am able to make the compilation error go away if I do any of the following:
- Remove inner classes (
IndexedSeq.empty[String]instead ofIndexedSeq.empty[Value]) - Remove existential types (
Outer[String]instead ofOuter[_]) - Remove IndexedSeq.apply (
values.headinstead ofvalues(0)) - Change
def innertoval inner(this is the most puzzling one)
Unfortunately in my use case, I can’t change any of those (it’s not evident why in this small example, but the actual code relies on them being that way). Am I doing something forbidden, or is this a limitation of the compiler?
Probably a limitation since it seems fine in
2.10.0-M4.Of course it gives a
NullPointerthough sinceouteris set tonull.