scala - 为什么 Buffer 不是 IndexedSeq 的子类?

标签 scala scala-collections

在scala集合库中Buffer继承自Seq:

Buffer[A] extends Seq[A] with GenericTraversableTemplate[A, Buffer] with BufferLike[A, Buffer[A]] with scala.Cloneable

并且 Buffer 文档说:

Buffers are used to create sequences of elements incrementally by appending, prepending, or inserting new elements. It is also possible to access and modify elements in a random access fashion via the index of the element in the current sequence.

IndexedSeq文档说:

A base trait for indexed sequences.

Indexed sequences support constant-time or near constant-time element access and length computation. They are defined in terms of abstract methods apply for indexing and length.

Indexed sequences do not add any new methods to Seq, but promise efficient implementations of random access patterns.

由于 Buffer 已经扩展了 Seq 并且 IndexedSeq 没有向 Seq 添加任何方法 Buffer 必须已经实现了 IndexedSeq 接口(interface),并且根据文档 它应该满足IndexedSeq的非功能性要求。 那么为什么 Buffer 不是 IndexedSeq

最佳答案

Buffer 不是 IndexedSeq,因为它不保证接近恒定时间的元素访问和长度计算。例如,ListBuffer 都不支持,正如您在 description of the performance characteristics of Scala collections 中看到的那样。 .

关于scala - 为什么 Buffer 不是 IndexedSeq 的子类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15142708/

相关文章:

scala - 类型中的尾随逗号

scala - 如何在 Scala 中按键对字典进行排序?

java - Scala 删除通用类型信息

scala - Spark MergeSchema 在 Parquet 列上

scala - 如何使用 Slick 代码生成器来包含数据库 View ?

scala - 为什么在 Scala 中使用存在类型时会忽略类型参数的边界?

performance - Scala:可变序列中最快的 `remove(i: Int)`

scala - 编写通用的 'fill'方法

Scala - 从 List 中获取唯一值

scala - Scala的语义可遍历,可迭代,序列,流和 View ?