Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I implement a collection in Scala 2.8?

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.

like image 769
Simon Reinhardt Avatar asked Mar 28 '10 22:03

Simon Reinhardt


1 Answers

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.

like image 154
Martin Odersky Avatar answered Nov 15 '22 21:11

Martin Odersky