为什么包scala.collections.immutable中的VectorBuilder是? [英] Why is VectorBuilder in the package scala.collections.immutable?
问题描述
VectorBuilder
在与 Vector
相同的源文件中定义。 Vector
是不可变的,并且在 scala.collections.immutable 包中,因此生成器位于同一包中。
VectorBuilder
is defined in the same source file as Vector
. Vector
is immutable and in the scala.collections.immutable package, so as a consequence the builder is in the same package.
据我所知, CanBuildFrom
使用的是 VectorBuilder $ c $如果未明确键入返回类型,则默认为c>。
- 是否有没有在单独文件中包含构建器的原因
- 构建器不是要直接使用吗?如果是这样,将使用哪个生成器或缓冲区来创建
Seq
?
- Is there a reason for not having the builder in a separate file in the mutable package?
- Is the builder not meant to be used directly? If so, which builder or buffer is to be used to create a
Seq
?
推荐答案
VectorBuilder
不能直接使用。如果要获取 Vector
的生成器,则只需调用 Vector.newBuilder [T]
,返回 Builder [T,Vector [T]]
(基础实例为 VectorBuilder
)。
VectorBuilder
is not meant to be used directly. If you want to get a builder for a Vector
, you only need to call Vector.newBuilder[T]
, which returns a Builder[T, Vector[T]]
(with the underlying instance being a VectorBuilder
).
因此,如果您想要用于创建 Seq
的默认构建器,则只需调用 Seq.newBuilder
:
So if you want the default builder that would be used to create a Seq
, you only need to call Seq.newBuilder
:
scala> Seq(1,2,3)
res0: Seq[Int] = List(1, 2, 3)
scala> Seq.newBuilder[Int]
res1: scala.collection.mutable.Builder[Int,Seq[Int]] = ListBuffer()
scala> Seq.newBuilder[Int].result
res2: Seq[Int] = List()
上面的代码显示了 Seq
的默认实现是列表,并且在逻辑上, Seq
的默认构建器是实际上是 mutable.ListBuffer
。
The above shows that the default implementation of Seq
is list, and, logically, the default builder for a Seq
is actually a mutable.ListBuffer
.
ListBuffer
更多不仅仅是 List
生成器,这就是为什么它位于 collection.mutable
而 VectorBuilder
不是 Buffer
,除构建 Vector
之外,不能用于其他任何用途。这可能就是为什么在 Vector
中本地定义它的原因。我不确定为什么它不是 private
,我无法在 Vector
本身的公共API中看到它的引用。 。也许应该是(私人)。
ListBuffer
is more than just a List
builder, that's why it is in collection.mutable
whereas VectorBuilder
is not a Buffer
, it cannot be used for anything else other than build a Vector
. That's probably why it is defined locally in Vector
. I am not sure why it isn't private
, I cannot see it referenced anywhere in the public API of Vector
itself. Maybe it should be (private).
仅供参考, CanBuildFrom
,几乎总是通过上面的 newBuilder
:
Just for reference, there is no hidden magic happening with CanBuildFrom
, it almost always just goes through the newBuilder
above:
-
当您未指定期望的集合类型时,例如
Seq(1,2).map(_ + 1)
,则唯一可用CanBuildFrom
来自伴侣对象一个Seq
,类型为CanBuildFrom [Seq [_] ,T,Seq [T]]
。这意味着结果也将是Seq
。
When you do not specify the expected collection type, as in
Seq(1,2).map(_+1)
, the only availableCanBuildFrom
comes from the companion object aSeq
, and is of typeCanBuildFrom[Seq[_], T, Seq[T]]
. That means the result will be aSeq
too.
与集合的大多数随播对象一样, CanBuildFrom
实例 Seq
仅提供一件事:调用 Seq.newBuilder
(在中定义GenTraversableFactory ...)
Like most companion objects of collections, the CanBuildFrom
instance Seq
provides only does one thing: call Seq.newBuilder
(that's defined in GenTraversableFactory ...)
这就是为什么 CanBuildFrom $ c $
Vector
的c>使用 VectorBuilder
。例如,在此:
That's why CanBuildFrom
for Vector
uses a VectorBuilder
. For example, in this:
scala> Vector(1,2,3).map(_+1)
res12: scala.collection.immutable.Vector[Int] = Vector(2, 3, 4)
使用的构建器是:
scala> implicitly[CanBuildFrom[Vector[Int], Int, Vector[Int]]].apply()
res13: scala.collection.mutable.Builder[Int,Vector[Int]] =
scala.collection.immutable.VectorBuilder@43efdf93
这篇关于为什么包scala.collections.immutable中的VectorBuilder是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!