From 2c28c5a8e824a3e1549c1a547a5978b93a71923c Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Wed, 8 Jul 2015 16:05:25 +0300 Subject: [PATCH] Define plusAssign operator for mutable collections. #KT-4020 Fixed --- .../kotlin/collections/MutableCollections.kt | 22 +++++++++++++++++++ .../stdlib/test/collections/CollectionTest.kt | 6 +++++ 2 files changed, 28 insertions(+) diff --git a/libraries/stdlib/src/kotlin/collections/MutableCollections.kt b/libraries/stdlib/src/kotlin/collections/MutableCollections.kt index 534d69ad933..1f3db47782b 100644 --- a/libraries/stdlib/src/kotlin/collections/MutableCollections.kt +++ b/libraries/stdlib/src/kotlin/collections/MutableCollections.kt @@ -1,5 +1,27 @@ package kotlin +/** + * Adds the specified [element] to this mutable collection. + */ +public fun MutableCollection.plusAssign(element: T) { + this.add(element) +} + +/** + * Adds all elements of the given [collection] to this mutable collection. + */ +public fun MutableCollection.plusAssign(collection: Iterable) { + this.addAll(collection) +} + +/** + * Adds all elements of the given [array] to this mutable collection. + */ +public fun MutableCollection.plusAssign(array: Array) { + this.addAll(array) +} + + /** * Adds all elements of the given [iterable] to this [MutableCollection]. */ diff --git a/libraries/stdlib/test/collections/CollectionTest.kt b/libraries/stdlib/test/collections/CollectionTest.kt index 1f7d5d4803b..e7ae486b969 100644 --- a/libraries/stdlib/test/collections/CollectionTest.kt +++ b/libraries/stdlib/test/collections/CollectionTest.kt @@ -233,6 +233,12 @@ class CollectionTest { l += arrayOf("cheese", "wine") assertEquals(listOf("cheese", "foo", "beer", "cheese", "wine"), l) assertTrue(l !== lOriginal) + + val ml = arrayListOf("cheese") + ml += "foo" + ml += listOf("beer") + ml += arrayOf("cheese", "wine") + assertEquals(l, ml) } test fun requireNoNulls() {