This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Right way of implementing filter on a hierarchy of collection

1 reply
edmondo1984
Joined: 2011-09-14,
User offline. Last seen 28 weeks 3 days ago.
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
dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: Right way of implementing filter on a hierarchy of collecti

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.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland