Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding mutable Seq

I'm pretty new to Scala and try to understand mutable Seq. Since it's in package mutable I expected there is a method that allows us to append element without copying the whole collection.

But there is no += method in the mutable.Seq, but in Buffer is. :+ and +: both copy the collection.

So why is it mutable?

like image 239
St.Antario Avatar asked Jun 07 '17 14:06

St.Antario


People also ask

Is Seq mutable?

mutable. Seq has update , that allows you to change the element at a given index, but it does not grow or shrink. Buffer is s specialization of Seq , that is both mutable and growable.

Is Scala Seq mutable?

Scala's default Seq class is mutable. Yes, you read that right.

What does seq mean in Scala?

Scala Seq is a trait to represent immutable sequences. This structure provides index based access and various utility methods to find elements, their occurences and subsequences. A Seq maintains the insertion order.


1 Answers

Because mutable and growable isn't the same thing. (the latter is one specific type of the former: everything, that's growable is mutable, but not everything that's mutable is growable).

mutable.Seq has update, that allows you to change the element at a given index, but it does not grow or shrink. Buffer is s specialization of Seq, that is both mutable and growable.

like image 62
Dima Avatar answered Oct 13 '22 12:10

Dima