Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala Collections inconsistencies

Why is there a lack of consistency between Sets and Lists in Scala Collections API?

For example, there is immutable Set, but also a mutable one. If I want to use the latter, I can simply do this:

val set = Set[A]()
set += new A

However, there is no mutable List, per se. If I want to write a similar code snippet using Lists, which data structure to use? LinkedList sounds as a good candidate, because it is mutable, but has no += method defined. ListBuffer seems to satisfy the requirements, but it is not a list.

After reading 2.8 Collections docs I come to the conclusion MutableList is probably the best fit.

I still somehow wish there was scala.collection.mutable.List.

like image 459
Nermin Serifovic Avatar asked Nov 03 '10 22:11

Nermin Serifovic


1 Answers

The reason for this is that Java has co-opted the functional List type to mean something that it is not (i.e. java.util.List is not a list).

It probably makes no sense for a functional programming language to have a mutable List as such a type is an oxymoron. Hence ListBuffer or ArrayBuffer. Or just use IndexedSeq, of which there are mutable and immutable implementations

like image 52
oxbow_lakes Avatar answered Oct 05 '22 20:10

oxbow_lakes