Make Map.plus accept Map out-projected by key type as either operand (receiver or parameter).

#KT-11301 Fixed
This commit is contained in:
Ilya Gorbunov
2016-03-30 23:01:49 +03:00
parent 0332217b70
commit d5d19f5595
3 changed files with 23 additions and 7 deletions
+2 -2
View File
@@ -22,10 +22,10 @@ public fun <E> HashSet(c: Collection<E>): HashSet<E>
public fun <E> LinkedHashSet(c: Collection<E>): HashSet<E>
= LinkedHashSet<E>(c.size).apply { addAll(c) }
public fun <K, V> HashMap(m: Map<K, V>): HashMap<K, V>
public fun <K, V> HashMap(m: Map<out K, V>): HashMap<K, V>
= HashMap<K, V>(m.size).apply { putAll(m) }
public fun <K, V> LinkedHashMap(m: Map<K, V>): LinkedHashMap<K, V>
public fun <K, V> LinkedHashMap(m: Map<out K, V>): LinkedHashMap<K, V>
= LinkedHashMap<K, V>(m.size).apply { putAll(m) }
public fun <E> ArrayList(c: Collection<E>): ArrayList<E>
@@ -404,31 +404,31 @@ public fun <K, V, M : MutableMap<in K, in V>> Sequence<Pair<K, V>>.toMap(destina
/**
* Creates a new read-only map by replacing or adding an entry to this map from a given key-value [pair].
*/
public operator fun <K, V> Map<K, V>.plus(pair: Pair<K, V>): Map<K, V>
public operator fun <K, V> Map<out K, V>.plus(pair: Pair<K, V>): Map<K, V>
= LinkedHashMap(this).apply { put(pair.first, pair.second) }
/**
* Creates a new read-only map by replacing or adding entries to this map from a given collection of key-value [pairs].
*/
public operator fun <K, V> Map<K, V>.plus(pairs: Iterable<Pair<K, V>>): Map<K, V>
public operator fun <K, V> Map<out K, V>.plus(pairs: Iterable<Pair<K, V>>): Map<K, V>
= LinkedHashMap(this).apply { putAll(pairs) }
/**
* Creates a new read-only map by replacing or adding entries to this map from a given array of key-value [pairs].
*/
public operator fun <K, V> Map<K, V>.plus(pairs: Array<out Pair<K, V>>): Map<K, V>
public operator fun <K, V> Map<out K, V>.plus(pairs: Array<out Pair<K, V>>): Map<K, V>
= LinkedHashMap(this).apply { putAll(pairs) }
/**
* Creates a new read-only map by replacing or adding entries to this map from a given sequence of key-value [pairs].
*/
public operator fun <K, V> Map<K, V>.plus(pairs: Sequence<Pair<K, V>>): Map<K, V>
public operator fun <K, V> Map<out K, V>.plus(pairs: Sequence<Pair<K, V>>): Map<K, V>
= LinkedHashMap(this).apply { putAll(pairs) }
/**
* Creates a new read-only map by replacing or adding entries to this map from another [map].
*/
public operator fun <K, V> Map<K, V>.plus(map: Map<K, V>): Map<K, V>
public operator fun <K, V> Map<out K, V>.plus(map: Map<out K, V>): Map<K, V>
= LinkedHashMap(this).apply { putAll(map) }
@@ -337,6 +337,22 @@ class MapTest {
@test fun plusMap() = testPlus { it + mapOf("C" to 3, "B" to 4) }
@test fun plusAny() {
testPlusAny(emptyMap<String, String>(), 1 to "A")
testPlusAny(mapOf("A" to null), "A" as CharSequence to 2)
}
fun <K, V> testPlusAny(mapObject: Any, pair: Pair<K, V>) {
val map = mapObject as Map<*, *>
fun assertContains(map: Map<*, *>) = assertEquals(pair.second, map[pair.first])
assertContains(map + pair)
assertContains(map + listOf(pair))
assertContains(map + arrayOf(pair))
assertContains(map + sequenceOf(pair))
assertContains(map + mapOf(pair))
}
fun testIdempotent(operation: (Map<String, Int>) -> Map<String, Int>) {