Suppose I want to add functionality like map to a Scala List, something along the lines of list mapmap f, which applies the function f to each element of list twice. (A more serious example might be implementing a parallel or distributed map, but I don’t want to get distracted by details in that direction.)
My first approach would be
object MapMap {
implicit def createFancyList[A](list: List[A]) = new Object {
def mapmap(f: A => A): List[A] = { list map { a: A => f(f(a)) } }
}
}
this now works great
scala> import MapMap._
import MapMap._
scala> List(1,2,3) mapmap { _ + 1 }
res1: List[Int] = List(3, 4, 5)
except of course it’s only for Lists, and there’s no reason we shouldn’t want this to work for anything Traverseable, with a map function, e.g. Sets or Streams. So the second attempt looks like
object MapMap2 {
implicit def createFancyTraversable[A](t: Traversable[A]) = new Object {
def mapmap(f: A => A): Traversable[A] = { t map { a: A => f(f(a)) } }
}
}
But now, of course, the result can’t be assigned to a List[A]:
scala> import MapMap2._
import MapMap2._
scala> val r: List[Int] = List(1,2,3) mapmap { _ + 1 }
<console>:9: error: type mismatch;
found : Traversable[Int]
required: List[Int]
Is there some middle ground? Can I write an implicit conversion that adds a method to all subclasses of Traversable, and successfully returns objects with that type?
(I’m guess this involves understanding the dreaded CanBuildFrom trait, and maybe even breakout!)
You can’t do this for all Traversables, as they don’t guarantee that map returns anything more specific than Traversable.See Update 2 below.UPDATE
I’ve added another pimped method,
mapToString, to demonstrate whyTraversableWaccepts two type parameters, rather than one parameter as in Alexey’s solution. The parameterCCis a higher kinded type, it represents the container type of the original collection. The second parameter,A, represents the element type of the original collection. The methodmapToStringis thus able to return the original container type with a different element type:CC[String.UPDATE 2
Thanks to @oxbow_lakes comment, I’ve rethought this. It is indeed possible to directly pimp
CC[X] <: Traversable[X],TraversableLikeis not strictly needed. Comments inline:What’s the difference? We had to use
collection.breakOut, because we can’t recover the specific collection subtype from a mereTraversable[A].The
Builderbis initialized with the original collection, which is the mechanism to preserve the dynamic type through amap. However, ourCanBuildFromdisavowed all knowledge of the From, by way of the type argumentNothing. All you can do withNothingis ignore it, which is exactly whatbreakOutdoes:We can’t call
b.apply(from), no more than you could calldef foo(a: Nothing) = 0.