views:

421

answers:

1

In trying to write an API I'm struggling with Scala's collections in 2.8(.0-beta1).

Basically what I need is to write something that:

  • adds functionality to immutable sets of a certain type
  • where all methods like filter and map return a collection of the same type without having to override everything (which is why I went for 2.8 in the first place)
  • where all collections you gain through those methods are constructed with the same parameters the original collection had (similar to how SortedSet hands through an ordering via implicits)
  • which is still a trait in itself, independent of any set implementations.

Additionally I want to define a default implementation, for example based on a HashSet. The companion object of the trait might use this default implementation. I'm not sure yet if I need the full power of builder factories to map my collection type to other collection types.

I read the paper on the redesign of the collections API but it seems like things have changed a bit since then and I'm missing some details in there. I've also digged through the collections source code but I'm not sure it's very consistent yet.

Ideally what I'd like to see is either a hands-on tutorial that tells me step-by-step just the bits that I need or an extensive description of all the details so I can judge myself which bits I need. I liked the chapter on object equality in "Programming in Scala". :-)
But I appreciate any pointers to documentation or examples that help me understand the new collections design better.

+15  A: 

I'd have a look at the implementation of collection.immutable.BitSet. It's a bit spread out, reusing things from collection.BitSetLike and collection.generic.BitSetFactory. But it does exactly what you specified: implement an immutable set of a certain element type that adds new functionality.

Martin Odersky
Thanks for the pointer, Martin, I will look into BitSet. It seems clearer than SortedSet. What I actually want to write is something that builds on top of sets but is independent of any implementations of sets. I then want another class that actually implements my trait by combining it with a set implementation. I guess the choice here is to either mixin or forward.
Simon Reinhardt