diff --git a/libraries/stdlib/src/kotlin/collections/MapWithDefault.kt b/libraries/stdlib/src/kotlin/collections/MapWithDefault.kt index 1a5e4406842..4eba8825b25 100644 --- a/libraries/stdlib/src/kotlin/collections/MapWithDefault.kt +++ b/libraries/stdlib/src/kotlin/collections/MapWithDefault.kt @@ -17,7 +17,7 @@ public fun Map.getOrImplicitDefault(key: K): V { if (this is MapWithDefault) return this.getOrImplicitDefault(key) - return getOrElse(key, { throw NoSuchElementException("Key $key is missing in the map.") }) + return getOrElseNullable(key, { throw NoSuchElementException("Key $key is missing in the map.") }) } @kotlin.jvm.JvmName("getOrImplicitDefaultNullable") @@ -26,7 +26,7 @@ public fun Map.getOrImplicitDefault(key: K): V { if (this is MapWithDefault) return this.getOrImplicitDefault(key) - return getOrElse(key, { throw NoSuchElementException("Key $key is missing in the map.") }) + return getOrElseNullable(key, { throw NoSuchElementException("Key $key is missing in the map.") }) } /** @@ -83,7 +83,7 @@ private class MapWithDefaultImpl(public override val map: Map, pr override val values: Collection get() = map.values override val entries: Set> get() = map.entries - override fun getOrImplicitDefault(key: K): V = map.getOrElse(key, { default(key) }) + override fun getOrImplicitDefault(key: K): V = map.getOrElseNullable(key, { default(key) }) } private class MutableMapWithDefaultImpl(public override val map: MutableMap, private val default: (key: K) -> V): MutableMapWithDefault { @@ -104,6 +104,6 @@ private class MutableMapWithDefaultImpl(public override val map: MutableMa override fun putAll(m: Map) = map.putAll(m) override fun clear() = map.clear() - override fun getOrImplicitDefault(key: K): V = map.getOrElse(key, { default(key) }) + override fun getOrImplicitDefault(key: K): V = map.getOrElseNullable(key, { default(key) }) } diff --git a/libraries/stdlib/src/kotlin/collections/Maps.kt b/libraries/stdlib/src/kotlin/collections/Maps.kt index 6567958735d..c7754f70fda 100644 --- a/libraries/stdlib/src/kotlin/collections/Maps.kt +++ b/libraries/stdlib/src/kotlin/collections/Maps.kt @@ -170,8 +170,16 @@ public fun Map.Entry.toPair(): Pair = Pair(key, value) * * @sample test.collections.MapTest.getOrElse */ -@Deprecated("This function will change its behavior soon not to distinguish missing keys and keys mapped to nulls. To stick with the new behavior you can use get(key) with ?: operator after instead.") public inline fun Map.getOrElse(key: K, defaultValue: () -> V): V { + val value = get(key) + if (value == null) { + return defaultValue() + } else { + return value + } +} + +internal inline fun Map.getOrElseNullable(key: K, defaultValue: () -> V): V { val value = get(key) if (value == null && !containsKey(key)) { return defaultValue() @@ -180,6 +188,8 @@ public inline fun Map.getOrElse(key: K, defaultValue: () -> V): V { } } + + /** * Returns the value for the given key. If the key is not found in the map, calls the [defaultValue] function, * puts its result into the map under the given key and returns it. diff --git a/libraries/stdlib/test/collections/MapTest.kt b/libraries/stdlib/test/collections/MapTest.kt index 13fce7fdf7e..357f5cb3365 100644 --- a/libraries/stdlib/test/collections/MapTest.kt +++ b/libraries/stdlib/test/collections/MapTest.kt @@ -20,6 +20,10 @@ class MapTest { val empty = mapOf() val c = empty.getOrElse("") { null } assertEquals(null, c) + + val nullable = mapOf(1 to null) + val d = nullable.getOrElse(1) { "x" } + assertEquals("x", d) } @test fun getOrImplicitDefault() {