我很惊讶地发现使用to[Col]
,Seq
的默认实现是Vector
而不是List
:
val x = Seq(1, 2, 3) // -> List(1, 2, 3)
x.toSeq // -> List(1, 2, 3)
x.to[Seq] // -> Vector(1, 2, 3)
怎么回事?
我很惊讶地发现使用to[Col]
,Seq
的默认实现是Vector
而不是List
:
val x = Seq(1, 2, 3) // -> List(1, 2, 3)
x.toSeq // -> List(1, 2, 3)
x.to[Seq] // -> Vector(1, 2, 3)
to
方法定义在GenTraversableOnce[A]
上:
def to[Col[_]](implicit cbf: CanBuildFrom[Nothing, A, Col[A]]): Col[A]
GenTraversableOnce
没有Repr
类型。因此:implicitly[CanBuildFrom[Nothing, Int, Seq[Int]]].apply.result // Vector()
而
implicitly[CanBuildFrom[Seq[Int], Int, Seq[Int]]].apply.result // List()
实际上,我认为这是一个bug。只有将该方法移动到GenTraversableLike
才能解决它。你有什么看法?
CanBuildFrom
,并且将使用to(Seq)
。 - 0__