1

I am working with a java library in scala, and I need to implement an Interface, which, among other things requires that I implement that I implement Java's Iterable interface.

When I try to implement it with or without JavaConversions it tells me that the method I am overriding (iterator(): java.util.Iterator) is of the wrong type, even though the method is of the type java.util.Iterator.

As a side note, could this be because the interface requires that I return an iterator of unspecified type?

Roman
  • 77
  • 2
  • 13

1 Answers1

0

The following works in the REPL:

import java.lang.Iterable
import java.util.Iterator

class A extends Iterable[String] {
    override def iterator(): Iterator[String] = null
}

Note that iterator() needs to return Iterator, not Iterable, and that confusingly, Iterable is in package java.lang while Iterator is in java.util.

Michał Kosmulski
  • 9,855
  • 1
  • 32
  • 51
  • This is not the problem. The parent class specifies an **Untyped** iterator, (effectively `Iterator>`). If the iterator were typed, I would not have encountered a problem. For reference the inheritance chain looks like this: **Java** interface Iterable -> **Java** interface Component extends Iterable (No Type) -> **Scala** class ScalaComponent extends Component. Thus, ScalaComponent has `override def iterator(): util.Iterator[_]`. This causes a compiler error. – Roman Feb 17 '14 at 20:43