The mutable Set has a method def +=(elem1: A, elem2: A, elems: A*): Set.this.type
but I can call this method with a single paramter:
val s = scala.collection.mutable.Set(1, 2)
s += 4
What method is actually called? There seems to be no overload for +=
with a single parameter. What is the intention of the above method signature?
The effect of = can also be used to easily understand what ++= would do. Since ++ generally represents concatenation of two collections, ++= would mean an "in place" update of a collection with another collection by concatenating the second collection to the first.
A set is a collection which only contains unique items which are not repeatable and a list is a collection which contains immutable data. In scala, ListSet class implements immutable sets using a list-based data structure.
In Java and Scala, certain builtin (a/k/a primitive) types get passed-by-value (e.g. int or Int) automatically, and every user defined type is passed-by-reference (i.e. must manually copy them to pass only their value).
Well, a little investigation here:
scala> val s = scala.collection.mutable.Set(1,2)
s: scala.collection.mutable.Set[Int] = Set(1, 2)
scala> s += 4
res0: s.type = Set(1, 2, 4)
scala> s +=
<console>:9: error: missing arguments for method += in trait SetLike;
follow this method with `_' if you want to treat it as a partially applied funct
ion
s +=
^
Oh, OK, so let's find the trait SetLike
docs:
abstract def +=(elem: A): SetLike.this.type
Adds a single element to the set.
And now it's clear it's an implementation of an abstract method in the mutable.SetLike
trait.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With