为什么在scala标准库中没有不可变数组? [英] Why no immutable arrays in scala standard library?
问题描述
Scala具有各种各样的不可变序列,例如List,Vector等.我很惊讶地发现没有实现由简单数组支持的不可变索引序列(Vector似乎太复杂了,以满足我的需求).
Scala has all sorts sorts of immutable sequences like List, Vector,etc. I have been surprised to find no implementation of immutable indexed sequence backed by a simple array (Vector seems way too complicated for my needs).
-
这是否有设计原因?我在邮件列表上找不到很好的解释.
Is there a design reason for this? I could not find a good explanation on the mailing list.
您是否建议使用不可变的索引序列,该序列具有与数组几乎相同的性能?我正在考虑scalaz的ImmutableArray,但是它在scala trunk中有一些问题.
Do you have a recommendation for an immutable indexed sequence that has close to the same performances as an array? I am considering scalaz's ImmutableArray, but it has some issues with scala trunk for example.
谢谢
推荐答案
您可以将数组转换为序列.
You could cast your array into a sequence.
val s: Seq[Int] = Array(1,2,3,4)
该数组将隐式转换为WrappedArray.并且由于类型为Seq,更新操作将不再可用.
The array will be implicitly converted to a WrappedArray. And as the type is Seq, update operations will no longer be available.
这篇关于为什么在scala标准库中没有不可变数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!