I saw some Scala code written as:
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].
What does :+ mean here?
I saw some Scala code written as:
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].
What does :+ mean here?
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.
There is as well colA ++: colB to 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.
++ and :++?:+ is a method on whatever type is returned by someVariable.next().
Presumably it's 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.
scala> List(1,2,3,4) :+ 400 res27: List[Int] = List(1, 2, 3, 4, 400)