Search code examples
scalascala-collectionsmutablelistscala-2.13

Scala 2.13 what to use instead of MutableList?


I'm upgrading a software from Scala 2.12.8 to Scala 2.13, and figure out that the collection MutableList (scala.collection.mutable.MutableList) was removed according to many guides (like this one).

This guide, for example, say that this was a deprecated collection so that's why it was removed, but I cannot find any deprecation in that class in previous versions.

"Deprecated collections were removed (MutableList, immutable.Stack, others)"

I also upgrade first to 2.12.9 (latest before 2.13.0) to check it there was any deprecated annotation giving a suggestion on what to use instead, but also in this version the collection is not deprecated.

I searched for this question I couldn't find a good answer. This question is gonna be good for me and also for future upgrades.

What should I use instead of MutableList, in Scala 2.13 ?


Solution

  • According to https://docs.scala-lang.org/overviews/core/collections-migration-213.html:

    collection.mutable.MutableList was not deprecated in 2.12 but was considered to be an implementation detail for implementing other collections. Use an ArrayDeque instead, or a List and a var.

    scala> val dq = new ArrayDeque[Int]
    dq: scala.collection.mutable.ArrayDeque[Int] = ArrayDeque()
    
    scala> dq.append(1)
    res1: dq.type = ArrayDeque(1)
    
    scala> dq.append(2)
    res2: dq.type = ArrayDeque(1, 2)
    
    scala> dq
    res3: scala.collection.mutable.ArrayDeque[Int] = ArrayDeque(1, 2)