mutable Set有方法def +=(elem1: A, elem2: A, elems: A*): Set.this.type
,但我可以用一个参数调用此方法:
val s = scala.collection.mutable.Set(1, 2)
s += 4
实际上叫什么方法?使用单个参数+=
似乎没有重载。上述方法签名的意图是什么?
答案 0 :(得分:9)
嗯,这里有点调查:
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 +=
^
哦,好的,让我们找到trait SetLike
docs:
abstract def +=(elem: A): SetLike.this.type
Adds a single element to the set.
现在很清楚它是mutable.SetLike
特征中抽象方法的实现。