Skip to content

Commit 58b44cf

Browse files
committed
Improve Growable.+= and related methods
- Deprecate the varargs version of `Growable.+=`. It is confusing when used with tuples (e.g. for `Map` types) and Scala 3.0 will probably drop support for infix operators with multiple parameters. - Add back the specialized two-argument `+=` to `AnyRefMap` which was present in 2.12. - There is no need to deprecate the two-argument versions now. If a future Scala version disallows calling them with operator syntax, these calls should automatically fall back to the tupled versions. - Make the tupled `addOne` methods in `AnyRefMap` and `LongMap` inlinable. When inlining is enabled the optimizer can elide the tupling. Upcoming inliner improvements should also enable this when `addOne` is called via the also inlinable `+=` alias.
1 parent 3a5ccd6 commit 58b44cf

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

library/src/scala/collection/mutable/AnyRefMap.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,9 +285,12 @@ class AnyRefMap[K <: AnyRef, V] private[collection] (defaultEntry: K => V, initi
285285
}
286286

287287
/** Adds a new key/value pair to this map and returns the map. */
288-
def addOne(key: K, value: V): this.type = { update(key, value); this }
288+
def +=(key: K, value: V): this.type = { update(key, value); this }
289289

290-
def addOne(kv: (K, V)): this.type = { update(kv._1, kv._2); this }
290+
/** Adds a new key/value pair to this map and returns the map. */
291+
@inline final def addOne(key: K, value: V): this.type = { update(key, value); this }
292+
293+
@inline override final def addOne(kv: (K, V)): this.type = { update(kv._1, kv._2); this }
291294

292295
def subtractOne(key: K): this.type = {
293296
val i = seekEntry(hashOf(key), key)

library/src/scala/collection/mutable/Growable.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ trait Growable[-A] extends Clearable {
3636
* @param elems the remaining elements to $add.
3737
* @return the $coll itself
3838
*/
39+
@deprecated("Use `++=` (addAll) instead of varargs `+=`", "2.13.0")
3940
@`inline` final def += (elem1: A, elem2: A, elems: A*): this.type = this += elem1 += elem2 ++= (elems: IterableOnce[A])
4041

4142
/** ${Add}s all elements produced by an IterableOnce to this $coll.

library/src/scala/collection/mutable/LongMap.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,10 @@ final class LongMap[V] private[collection] (defaultEntry: Long => V, initialBuff
342342
/** Adds a new key/value pair to this map and returns the map. */
343343
def +=(key: Long, value: V): this.type = { update(key, value); this }
344344

345-
override def addOne(kv: (Long, V)): this.type = { update(kv._1, kv._2); this }
345+
/** Adds a new key/value pair to this map and returns the map. */
346+
@inline final def addOne(key: Long, value: V): this.type = { update(key, value); this }
347+
348+
@inline override final def addOne(kv: (Long, V)): this.type = { update(kv._1, kv._2); this }
346349

347350
def subtractOne(key: Long): this.type = {
348351
if (key == -key) {

0 commit comments

Comments
 (0)