scala> import collection.breakOut import collection.breakOut scala> def foo: NodeSeq = data.map { s => <x>{s}</x> }(breakOut) foo: scala.xml.NodeSeq
There are actually two argument lists on the method map. The first accepts the function that you passed. The second accepts a CanBuildFrom object, which is used to create a builder, which then creates the return sequence. This argument is implicit, so usually the compiler populates it for you. It accepts 3 types of parameters: From, T, To. There are several preliminary restrictions (including in the NodeSeq object), but none of them correspond to From = Array, T = Node, To = NodeSeq.
breakOut solves this: it is a general method that returns an instance of CanBuildFrom by searching for the implicit CanBuildFrom [Nothing, T, To]. According to implicit search rules, any CanBuildFrom that matches T, To and has From> Nothing is acceptable. In this case: canBuildFrom in an Array object
Ittayd
source share