Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

A function of mine produces an array - an ordered, contiguously numbered set of records. But as far as I know Scala Array is a mutable collection, while functional approach suggests it would make more sense to return an immutable collection in a general case. So I just call Array.toIndexedSeq to return an IndexedSeq instead of 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?

like image 593
Ivan Avatar asked Feb 22 '23 14:02

Ivan


1 Answers

Can this be considered a correct thing to do?

Yes.

Doesn't it introduce any inobvious behaviour which can influence the function and the result usage and be probably considered undesirable?

No, not that I know of.

Are there any better practices for the issue?

If possible, try to eliminate the use of array altogether, unless of course the performance is paramount.

like image 156
missingfaktor Avatar answered Feb 25 '23 02:02

missingfaktor