“++=”在 Scala 中是什么意思
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22817146/
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 Michael
This is the implementationof flatMapin Scala
这是执行的flatMap斯卡拉
def flatMap[B, That](f: A => GenTraversableOnce[B])(implicit bf: CanBuildFrom[Repr, B, That]): That = {
def builder = bf(repr) // ...
val b = builder
for (x <- this) b ++= f(x).seq
b.result
}
What does ++=mean here ?
++=这里是什么意思?
回答by wingedsubmariner
++=can mean two different things in Scala:
++=在 Scala 中可能意味着两个不同的东西:
1: Invoke the ++=method
1:调用++=方法
In your example with flatMap, the ++=method of Buildertakes another collection and adds its elements into the builder. Many of the other mutable collections in the Scala collections library define a similiar ++=method.
在您的示例中flatMap, 的++=方法Builder采用另一个集合并将其元素添加到构建器中。Scala 集合库中的许多其他可变集合定义了类似的++=方法。
2: Invoke the ++method and replace the contents of a var
2:调用++方法并替换a的内容var
++=can also be used to invoke the ++method of an object in a varand replace the value of the varwith the result:
++=也可用于调用++a 中对象的方法,var并用var结果替换 的值:
var l = List(1, 2)
l ++= List(3, 4)
// l is now List(1, 2, 3, 4)
The line l ++= List(3, 4)is equivalent to l = l ++ List(3, 4).
该行l ++= List(3, 4)相当于l = l ++ List(3, 4)。
回答by AmigoNico
Note that ++=is a method, not a part of the Scala language. If it is defined for a particular class, then it has whatever meaning that class defines it to have. In this case it means "add these to the end."
请注意,这++=是一种方法,而不是 Scala 语言的一部分。如果它是为特定类定义的,那么它具有该类定义它具有的任何含义。在这种情况下,它的意思是“将这些添加到最后”。
Also note that if a class defines ++but not ++=then the compiler will treat
另请注意,如果一个类定义++但未定义,++=则编译器将处理
x ++= y
as
作为
x = x ++ y
this is true generally for symbols ending in an equal sign (other than ==, !=, and =, of course). This syntactic sugar allows immutable and mutable variants of the same data structure to be used in a consistent way.
这通常是真实的用于在等号结束符号(比其他==,!=以及=,当然)。这种语法糖允许以一致的方式使用相同数据结构的不可变和可变变体。
回答by Windor C
The API of Buildersays:
adds all elements produced by a TraversableOnce to this growable collection.
将 TraversableOnce 生成的所有元素添加到这个可增长的集合中。

![scala 播放 ScalaJSON Reads[T] 解析 ValidationError(error.path.missing,WrappedArray())](/res/img/loading.gif)