数组 – 如果Scala中需要不可变数组,返回IndexesSeq而不是Array是否正确?

前端之家收集整理的这篇文章主要介绍了数组 – 如果Scala中需要不可变数组,返回IndexesSeq而不是Array是否正确?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的一个函数产生一个数组 – 一组有序的,连续编号的记录.但据我所知,Scala Array是一个可变集合,而功能方法表明在一般情况下返回不可变集合会更有意义.所以我只是调用Array.toIndexedSeq来返回IndexedSeq而不是Array.这可以被认为是正确的做法吗?它是否会引入任何可能影响功能和结果使用的明显行为,并且可能被认为是不合需要的?这个问题有更好的做法吗?

解决方法

Can this be considered a correct thing to do?

是.

Doesn’t it introduce any inobvIoUs behavIoUr which can influence the
function and the result usage and be probably considered undesirable?

不,不是我所知道的.

Are there any better practices for the issue?

如果可能的话,尝试完全消除数组的使用,除非性能是最重要的.

猜你在找的Scala相关文章