hollow7 hollow7 - 4 months ago 15
Scala Question

Scala's += method for mutable collections

What does the += method for scale's mutable collections gets translated to behind the scenes?

For example, if I do += on a mutable queue, is that equivalent to doing an enqueue? Ditto for -=?

Answer

According to the source code, enqueue is implemented as:

def enqueue(elems: A*): Unit = this ++= elems

The dequeue code is more complicated and not equivalent to -= as there is no -= method for a Queue.