Understanding mutable Seq

北城以北 提交于 2019-12-23 06:58:42

问题


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?


回答1:


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.




回答2:


As explained in the documentation, mutable.Seq adds an update method to collection.Seq. += on the other hand is defined in Growable.

In Scala standard library, most mutable collections extend the immutable version, which is why they inherit copying :+, +:.



来源:https://stackoverflow.com/questions/44415432/understanding-mutable-seq

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!