Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why no immutable arrays in scala standard library?

Tags:

arrays

scala

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.

  • 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.

Thank you

like image 962
T. Hunter Avatar asked Jan 28 '11 07:01

T. Hunter


People also ask

Are arrays immutable in Scala?

Array in scala is homogeneous and mutable, i.e it contains elements of the same data type and its elements can change but the size of array size can't change.

Why is immutability important in Scala?

Immutable objects and data structures are first-class citizens in Scala. This is because they prevent mistakes in distributed systems and provide thread-safe data.

Which is immutable in Scala?

In Scala, all number types, strings, and tuples are immutable.

Is seq immutable in Scala?

Seq which represents sequences that are guaranteed immutable.


1 Answers

You could cast your array into a sequence.

val s: Seq[Int] = Array(1,2,3,4) 

The array will be implicitly converted to a WrappedArray. And as the type is Seq, update operations will no longer be available.

like image 157
Monkey Avatar answered Oct 11 '22 17:10

Monkey