Map.getOrPut: treat nulls as missing values.

This commit is contained in:
Ilya Gorbunov
2016-01-22 07:47:54 +03:00
parent a49db730a9
commit 23080f78f7
8 changed files with 21 additions and 44 deletions
-12
View File
@@ -35,15 +35,3 @@ public fun <T> setOf(element: T): Set<T> = hashSetOf(element)
* specified value.
*/
public fun <K, V> mapOf(pair: Pair<K, V>): Map<K, V> = hashMapOf(pair)
public inline fun <K, V> MutableMap<K, V>.getOrPut(key: K, defaultValue: () -> V): V {
val value = get(key)
return if (value == null && !containsKey(key)) {
val answer = defaultValue()
put(key, answer)
answer
} else {
value as V
}
}