开发者

How do I implement a collection in Scala 2.8?

开发者 https://www.devze.com 2022-12-24 18:19 出处:网络
In trying to write a开发者_开发知识库n API I\'m struggling with Scala\'s collections in 2.8(.0-beta1).

In trying to write a开发者_开发知识库n 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消