Is it right to return IndexesSeq instead of Array if an immutable array is needed in Scala?

The my function produces an array - an ordered, contiguously numbered set of records. But as far as I know, Scala Array is a mutable collection, while the functional approach assumes that it makes sense to return an immutable collection in the general case. So I just call Array.toIndexedSeq to return IndexedSeq instead of Array . Can this be considered the right thing? Does it introduce any non-obvious behavior that may affect the function and use of the result and is probably considered undesirable? Are there more effective methods to solve the problem?

+4
source share
2 answers

Can this be considered the right thing?

Yes.

Does it introduce any non-obvious behavior that may affect the functions and uses of the result and are probably considered undesirable?

No, not what I know.

Are there any improvements to this issue?

If possible, try to completely eliminate the use of an array, unless, of course, performance is of utmost importance.

+4
source

The only possible non-obvious thing that I can think of is that array.toIndexedSeq does not create a simple wrapper on top of the array itself, as the Java Collections.unmodifiable* methods do, but copies the elements to a new collection. (Otherwise, subsequent changes to the array may cause the "immutable" sequence to mutate suddenly.)

+4
source

Source: https://habr.com/ru/post/1395089/


All Articles