September 2018
Intermediate to advanced
398 pages
9h 43m
English
The definition of several methods on the Scala collection API, such as map, has an implicit parameter of a CanBuildFrom type. This type is used to build a collection of the same type as the input type.
For instance, when you call map on a Vector, the return type will still be a Vector, as shown in the following code run in the REPL:
val vec = Vector("hello", "world").map(s => s -> s.length)// vec: scala.collection.immutable.Vector[(String, Int)] = // Vector((hello,5), (world,5))
When you position your cursor in IntelliJ at the map method and press cmd + left-click, you will see that map is declared in TraversableLike, as follows:
def map[B, That](f: A => B)(implicit bf: CanBuildFrom[Repr, B, That]): That = {...}
The TraversableLike ...
Read now
Unlock full access