Define plusAssign operator for mutable collections.

#KT-4020 Fixed
This commit is contained in:
Ilya Gorbunov
2015-07-08 16:05:25 +03:00
parent dd945f4d1a
commit 2c28c5a8e8
2 changed files with 28 additions and 0 deletions
@@ -1,5 +1,27 @@
package kotlin
/**
* Adds the specified [element] to this mutable collection.
*/
public fun <T> MutableCollection<in T>.plusAssign(element: T) {
this.add(element)
}
/**
* Adds all elements of the given [collection] to this mutable collection.
*/
public fun <T> MutableCollection<in T>.plusAssign(collection: Iterable<T>) {
this.addAll(collection)
}
/**
* Adds all elements of the given [array] to this mutable collection.
*/
public fun <T> MutableCollection<in T>.plusAssign(array: Array<T>) {
this.addAll(array)
}
/**
* Adds all elements of the given [iterable] to this [MutableCollection].
*/
@@ -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() {