Scala "cursor.asInstanceOf[::[A]].tl = newElem" what does it mean

34 views Asked by At

I am looking at the source code for Scala's mutable ListBuffer here https://github.com/scala/scala/blob/v2.11.8/src/library/scala/collection/mutable/ListBuffer.scala#L158

and I ran into this cast on line 158. I have been unable to figure out what .asInstanceOf[::[A]] means. :: is not a bounds notation that I could find in the documentation. But, it is a method for a List. Can anyone explain what this cast is doing?

1

There are 1 answers

0
adamwy On

:: is a list constructor that is composed of head and tail (as opposed to Nil which has neither). In other words it's a kind of list that guarantees that it contains at least one element (head).

You can find its definition in List.scala: https://github.com/scala/scala/blob/v2.11.8/src/library/scala/collection/immutable/List.scala#L439

So what this cast does is that it turns the cursor typed as List[A] into more specific ::[A], that allows accessing its tl variable, representing list tail. Which is necessary for efficient update operation.