gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 22:12:00 24 4
gpt4 key购买 nike

在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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com