Deil
Deil

Reputation: 512

Understanding ListBuffer's append operation in Scala. Mutable or immutable tail?

Here is a code from ListBuffers' append method:

def += (x: A): this.type = {
  ...
  last0 = new :: (x, Nil)
  last1.tl = last0   <-------- compiler is complaining Reassignment to val
  ...
}

When I ctrl+click on ColonColon class( which is essentially a List) I get

package scala.collection.immutable
@SerialVersionUID(509929039250432923L) // value computed by serialver for 2.11.2, annotation added in 2.11.4
final case class ::[B](override val head: B, private[scala] var tl: List[B]) extends List[B] {
  override def tail : List[B] = tl
  override def isEmpty: Boolean = false
}

with vaR tl. But when I click on .tl method I get

package scala.collection.immutable
@scala.SerialVersionUID(value = 509929039250432923)
final case class ::[B](override val head : B, private[scala] val tl : scala.collection.immutable.List[B]) extends scala.collection.immutable.List[B] with scala.Product with scala.Serializable {
  override def tail : scala.collection.immutable.List[B] = { /* compiled code */ }
  override def isEmpty : scala.Boolean = { /* compiled code */ }
}

with vaL tl.

Can someone explain what is going on here?

Upvotes: 1

Views: 52

Answers (0)

Related Questions