Define plusAssign operator for mutable collections.
#KT-4020 Fixed
This commit is contained in:
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user