deamon
deamon

Reputation: 92417

Why can I call += with a single parameter on a Scala Set?

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?

Upvotes: 5

Views: 118

Answers (1)

Patryk Ćwiek
Patryk Ćwiek

Reputation: 14318

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.

Upvotes: 9

Related Questions