Scala:如何找出哪个 CanBuildFrom 实现用于 StringOps.map

Scala: How to find out which CanBuildFrom implementation is used for StringOps.map

我正在通过书中的练习来学习 Scala "Scala for the Impatient"。一题问:

The result of "abc".map(_.toUpper) is a String, but the result of "abc".map(_.toInt) is a Vector. Find out why.

阅读“The Architecture of Scala Collections”后,我认为这是因为选择了不同的 CanBuildFrom 隐式实现。我怎样才能找出上面的表达式选择了哪些?

您可以使用 show{reify{...}} 来获取所有隐式,如下所示:

scala> import scala.reflect.runtime.{universe => ru}
import scala.reflect.runtime.{universe=>ru}

scala> ru.show{ ru.reify{ "abc".map(_.toUpper) }.tree }
res0: String = Predef.augmentString("abc").map(((x) => Predef.charWrapper(x).toUpper))(Predef.StringCanBuildFrom)

scala> ru.show{ ru.reify{ "123".map(_.toInt) }.tree }
res1: String = Predef.augmentString("123").map(((x) => x.toInt))(Predef.fallbackStringCanBuildFrom)

所以 Predef.StringCanBuildFrom 类型 CanBuildFrom[String,Char,String] 用于 _.toUpperPredef.fallbackStringCanBuildFrom 类型 CanBuildFrom[String,Int,IndexedSeq[Int]] 用于 _.toInt

scala> Predef.StringCanBuildFrom
res2: scala.collection.generic.CanBuildFrom[String,Char,String] = scala.Predef$$anon@43b7a6fa

scala> Predef.fallbackStringCanBuildFrom[Int]
res3: scala.collection.generic.CanBuildFrom[String,Int,scala.collection.immutable.IndexedSeq[Int]] = scala.LowPriorityImplicits$$anon@61af1aa0

请注意,类型不是 Vector,而是 IndexedSeqVector是具体实现。