I have the following class definition:
class Foo[T](iteratorThunk: () => Iterator[T]) {
def values = iteratorThunk()
}
And I would like Foo to have all the methods that Iterator exposes, while still returning objects of type Foo. For instance, I would like to be able to do:
val a = new Foo({ () => List(1, 2, 3).toIterator })
val b = new Foo({ () => List(4, 5, 6).toIterator })
val c = a ++ b
And have c equal to:
new Foo({ () => a.values ++ b.values })
I had a look at forwarders (IterableForwarder, TraversableForwarder, ...), but none seemed fit to forward methods to an Iterator, and I would still like to keep Foo as both the static and dynamic result type.
What is the best way to achieve this behavior, without defining every forwarding method?
Then you want either a
Traverableor anIterable. To have them work as you wish, you need to extend both them andTraversableLikeorIterableLike, through which you'll specify the return type. And you'll need to provide both aBuilderand aCanBuildFromas well.Here's a simple implementation: