看着
val sb = Seq.newBuilder[Int]
println(sb.getClass.getName)
sb += 1
sb += 2
val s = sb.result()
println(s.getClass.getName)
Run Code Online (Sandbox Code Playgroud)
输出是
scala.collection.mutable.ListBuffer
scala.collection.immutable.$ colon $ colon
使用Scala 2.10.1.
我希望Seq.newBuilder能回来一个VectorBuilder例子.这是由返回CanBuildFrom,如果结果是显式类型的Seq:
def build[T, C <: Iterable[T]](x: T, y: T)
(implicit cbf: CanBuildFrom[Nothing, T, C]): C = {
val b = cbf()
println(b.getClass.getName)
b += x
b += y
b.result()
}
val s: Seq[Int] = build(1, 2)
println(s.getClass.getName) // scala.collection.immutable.Vector
Run Code Online (Sandbox Code Playgroud)
在这种情况下,构建器是a VectorBuilder,结果的类是a Vector.
所以我明确地希望构建一个Seq,但结果是 …