- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Right way of implementing filter on a hierarchy of collection
Fri, 2012-01-13, 14:16
Dear All,
I am a little bit confused from self type and generics.
I have to implement a hiearchy of custom generic collection implementing a filter function, and of course I want that each collection when filtered return a collection of the same type.
What is the right way to implement this?
Best Regards
Edmondo
I am a little bit confused from self type and generics.
I have to implement a hiearchy of custom generic collection implementing a filter function, and of course I want that each collection when filtered return a collection of the same type.
What is the right way to implement this?
Best Regards
Edmondo
On Fri, Jan 13, 2012 at 11:16, Edmondo Porcu wrote:
> Dear All,
>
> I am a little bit confused from self type and generics.
>
> I have to implement a hiearchy of custom generic collection implementing a
> filter function, and of course I want that each collection when filtered
> return a collection of the same type.
>
> What is the right way to implement this?
The default implementation of "filter" in TraversableLike uses the
protected method "newBuilder". So you need three things:
1. Extend TraversableLike[A, YourCollectionHere]
2. Override this method:
protected[this] def newBuilder: Builder[A, Repr]
3. Create a Builder for your class.