[英]Scala “cursor.asInstanceOf[::[A]].tl = newElem” what does it mean
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 我在这里查看Scala可变ListBuffer的源代码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. 我在第158行遇到了这个问题。我一直无法弄清楚.asInstanceOf[::[A]]
是什么意思。 ::
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? 谁能解释这个演员在做什么?
::
is a list constructor that is composed of head and tail (as opposed to Nil
which has neither). ::
是一个由头和尾组成的列表构造函数(与Nil
都不一样)。 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 您可以在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. 因此,此强制转换的作用是将类型为List[A]
的cursor
转换为更特定的::[A]
,从而允许访问其tl
变量,表示列表尾部。 Which is necessary for efficient update
operation. 这对于有效的update
操作是必需的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.