I’m trying to define a custom collection interface in Scala 2.8. I want to require that subclasses be Traversable, plus some other behavior. I also want methods like map() to return the appropriate type, as below:
trait CustomCollection[+A] extends Traversable[A] {
def customOperation(i:Int):Int // for example
}
def incrementAll(c:CustomCollection[Int]):CustomCollection[Int] = c.map { _ + 1 }
This doesn’t compile, because CustomCollection.map() returns a Traversable. I suppose I need to define a CanBuildFrom, but then I need to define an apply() method that constructs an instance from scratch. I don’t want to specify a way to construct this; that should be up to the implementer. Is this possible?
If you want
mapto return a more specific collection type, then you should also inheritTraversableLike, with the second type parameter (representation type) set toCustomCollection[A].Next,
maprequires an implicit parameter of typeCanBuildFrom. It will look in the companion object ofCustomCollectionto find a conforming implicit value of that type. If you take a look at the source code ofSeqclasses, you will see that their companions provideCanBuildFromobjects of typeGenericCanBuildFromwhich forwards the call for the builder back to the collection that requested the builder. That way, the dynamic type of return type ofSeqtransformer methods (e.g.map) is always the same as the type of the sequence itself.What you have to do is:
CustomCollection[A]inheritTraversableLikeCustomCollection[A]inheritGenericTraversableTemplateCustomCollectionand add an implicit which returns aGenericCanBuildFromCustomCollectioncompanionThe implementers of
CustomCollectionwill need to provide companion objects which have builder implementations and implicitCanBuildFromobjects (which can simply beGenericCanBuildFroms).EDIT:
GenericTraversablTemplatementioned above is needed because it first ensures that the collection will have thegenericBuildermethod called by theGenericCanBuildFrombuilder factory. Second, it ensures that the collection actually has the companion object of typeGenericCompanion.