[英]What does “:+” mean in Scala
I saw some Scala code written as:我看到一些 Scala 代码写成:
def next(): Array[String] = someVariable.next() :+ iterator.key
Where someVariable
has a method next()
to get the next line and the iterator is of type Iterator[String]
.其中
someVariable
有一个方法next()
来获取下一行,并且迭代器的类型为Iterator[String]
。
What does :+
mean here? :+
在这里是什么意思?
On Scala Collections there is usually :+
and +:
.在 Scala 集合上,通常有
:+
和+:
。
Both add an element to the collection.两者都向集合添加一个元素。
:+
appends +:
prepends. :+
附加+:
前置。
A good reminder is, :
is where the Collection goes.一个很好的提醒是,
:
是 Collection 所在的位置。
There is as well colA ++: colB
to concat collections, where the :
side collection determines the resulting type.还有
colA ++: colB
来连接集合,其中:
侧集合确定结果类型。 If a :++
exists, it is the same as ++
.如果
:++
存在,则与++
相同。 In both cases the left side collection determines the type of result.在这两种情况下,左侧集合决定了结果的类型。
:+
is a method on whatever type is returned by someVariable.next()
. :+
是someVariable.next()
返回的任何类型的方法。
Presumably it's scala.Array.:+
大概是
scala.Array.:+
A copy of this array with an element appended.
此数组的副本,并附加了一个元素。
This is also a case where an IDE would help you greatly.这也是 IDE 会极大帮助您的情况。 With Intellij for example, you could use the "Quick doc" or "Jump to definition" commands on
:+
and immediately find out where it came from.例如,使用 Intellij,您可以在
:+
上使用“快速文档”或“跳转到定义”命令,并立即找出它的来源。 I've found that tooling to be invaluable in writing scala.我发现该工具在编写 scala 时非常宝贵。
scala> List(1,2,3,4) :+ 400
res27: List[Int] = List(1, 2, 3, 4, 400)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.