":+" 在 Scala 中是什么意思
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37190619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does ":+" mean in Scala
提问by jlp
I saw some Scala code written as:
我看到一些 Scala 代码写成:
def next(): Array[String] = someVariable.next() :+ iterator.key
Where someVariablehas a method next()to get the next line and the iterator is of type Iterator[String].
WheresomeVariable有next()获取下一行的方法,并且迭代器的类型为Iterator[String]。
What does :+mean here?
:+这里是什么意思?
回答by Nabil A.
On Scala Collections there is usually :+and +:.
Both add an element to the collection. :+appends +:prepends.
A good reminder is, :is where the Collection goes.
在 Scala 集合上,通常有:+和+:。
两者都向集合添加一个元素。:+附加+:前置。
一个很好的提醒是,这:是集合的去向。
There is as well colA ++: colBto concat collections, where the :side collection determines the resulting type.
If a :++exists, it is the same as ++. In both cases the left side collection determines the type of result.
还有colA ++: colBconcat 集合,其中:侧集合确定结果类型。如果 a:++存在,则与 相同++。在这两种情况下,左侧集合决定了结果的类型。
回答by Daenyth
:+is a method on whatever type is returned by someVariable.next().
:+是someVariable.next().返回的任何类型的方法。
Presumably it's :T](elem:B)(implicitevidence$2:scala.reflect.ClassTag[B]):Array[B]" rel="noreferrer">scala.Array.:+
想必是 :T](elem:B)(implicitevidence$2:scala.reflect.ClassTag[B]):Array[B]" rel="noreferrer">scala.Array.:+
A copy of this array with an element appended.
此数组的副本,附加了一个元素。
This is also a case where an IDE would help you greatly. With Intellij for example, you could use the "Quick doc" or "Jump to definition" commands on :+and immediately find out where it came from. I've found that tooling to be invaluable in writing scala.
这也是 IDE 会极大帮助您的情况。例如,使用 Intellij,您可以使用“快速文档”或“跳转到定义”命令:+并立即找出它的来源。我发现该工具在编写 scala 时非常宝贵。
回答by user1668782
scala> List(1,2,3,4) :+ 400
res27: List[Int] = List(1, 2, 3, 4, 400)

