vectorbuilder
defined in same source file vector
. vector
immutable , in scala.collections.immutable package, consequence builder in same package.
as far can tell, canbuildfrom
uses vectorbuilder
default, if return type not explicitly typed.
- is there reason not having builder in separate file in mutable package?
- is builder not meant used directly? if so, builder or buffer used create
seq
?
vectorbuilder
not meant used directly. if want builder vector
, need call vector.newbuilder[t]
, returns builder[t, vector[t]]
(with underlying instance being vectorbuilder
).
so if want default builder used create seq
, need 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()
the above shows default implementation of seq
list, and, logically, default builder seq
mutable.listbuffer
.
listbuffer
more list
builder, that's why in collection.mutable
whereas vectorbuilder
not buffer
, cannot used else other build vector
. that's why defined locally in vector
. not sure why isn't private
, cannot see referenced anywhere in public api of vector
itself. maybe should (private).
just reference, there no hidden magic happening canbuildfrom
, goes through newbuilder
above:
when not specify expected collection type, in
seq(1,2).map(_+1)
, availablecanbuildfrom
comes from companion objectseq
, , of typecanbuildfrom[seq[_], t, seq[t]]
. means resultseq
too.like companion objects of collections,
canbuildfrom
instanceseq
provides 1 thing: callseq.newbuilder
(that's defined in gentraversablefactory ...)
that's why canbuildfrom
vector
uses vectorbuilder
. example, in this:
scala> vector(1,2,3).map(_+1) res12: scala.collection.immutable.vector[int] = vector(2, 3, 4)
the builder used is:
scala> implicitly[canbuildfrom[vector[int], int, vector[int]]].apply() res13: scala.collection.mutable.builder[int,vector[int]] = scala.collection.immutable.vectorbuilder@43efdf93
Comments
Post a Comment