Scala 2.8 CanBuildFrom

时间秒杀一切 提交于 2019-11-27 03:27:57
Daniel C. Sobral

Note that the second argument to map is an implicit argument. There must be an implicit in scope with the appropriate types, or, otherwise, you must pass such an argument.

In your example, That must be Set[String], B must be Int and Repr must be List[String]. Therefore, for that to compile you need the following implicit object in scope:

implicit object X: CanBuildFrom[List[String], Int, Set[String]]

There's no such thing in scope. Also, breakOut can't provide it, because it, itself, needs an implicit CanBuildFrom, whose first type can be any class (a contra-variant descendant of Nothing), but otherwise restricted by the other types.

Take a look, for instance, on the CanBuildFrom factory from the companion object of List:

implicit def  canBuildFrom [A] : CanBuildFrom[List, A, List[A]]  

Because it binds the second and third parameters through A, the implicit in question won't work.

So, how does one know where to look for, regarding such implicits? First of all, Scala does import a few things into all scopes. Right now, I can recall the following imports:

import scala.package._ // Package object
import scala.Predef._  // Object
// import scala.LowPriorityImplicits, class inherited by Predef
import scala.runtime._ // Package

Since we are concerned about implicits, note that when you import things from packages, the only implicits possible are singletons. When you import things from objects (singletons), on the other hand, you can have implicit definitions, values and singletons.

Right now, there are CanBuildFrom implicits inside Predef and LowPriorityImplicits, which are concerned with strings. They enable us to write "this is a string" map (_.toInt).

So, barring these automatic imports, and the explicit imports you make, where else can an implicit be found? One place: the companion objects of the instance on which the method is being applied.

I say companion objects, in the plural, because the companion objects of all traits and classes inherited by the class of the instance in question may contain relevant implicits. I'm not sure if the instance itself may contain an implicit. To be honest, I can't reproduce this right now, so I'm certainly making a mistake of some kind here.

At any rate, look inside the companion objects.

object ArrayBuffer extends SeqFactory[ArrayBuffer] {
  /** $genericCanBuildFromInfo */
  implicit def canBuildFrom[A]: CanBuildFrom[Coll, A, ArrayBuffer[A]] = new GenericCanBuildFrom[A]
  def newBuilder[A]: Builder[A, ArrayBuffer[A]] = new ArrayBuffer[A]
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!