为什么 Scala 标准库中没有不可变数组? [英] Why no immutable arrays in scala standard library?

查看:28
本文介绍了为什么 Scala 标准库中没有不可变数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 主干.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆