5

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?

deamon
  • 89,107
  • 111
  • 320
  • 448

1 Answers1

9

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.

Patryk Ćwiek
  • 14,078
  • 3
  • 55
  • 76