From c8e711f84beae2a03fbaee1fef9e6cd73b2ebae1 Mon Sep 17 00:00:00 2001 From: Andrey Breslav Date: Thu, 27 Sep 2012 13:51:55 +0400 Subject: [PATCH] getOrElse() and getOrPut() fixed to respect how maps work with nulls --- libraries/stdlib/src/kotlin/Maps.kt | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/libraries/stdlib/src/kotlin/Maps.kt b/libraries/stdlib/src/kotlin/Maps.kt index 4e32aa9524c..7c4e78c516f 100644 --- a/libraries/stdlib/src/kotlin/Maps.kt +++ b/libraries/stdlib/src/kotlin/Maps.kt @@ -44,10 +44,9 @@ fun Map.Entry.component2() : V { * * @includeFunctionBody ../../test/MapTest.kt getOrElse */ -public inline fun Map.getOrElse(key: K, defaultValue: ()-> V?) : V? { - val current = this.get(key) - if (current != null) { - return current +public inline fun Map.getOrElse(key: K, defaultValue: ()-> V) : V { + if (this.containsKey(key)) { + return this.get(key) } else { return defaultValue() } @@ -59,9 +58,8 @@ public inline fun Map.getOrElse(key: K, defaultValue: ()-> V?) : V? { * @includeFunctionBody ../../test/MapTest.kt getOrElse */ public inline fun MutableMap.getOrPut(key: K, defaultValue: ()-> V) : V { - val current = this.get(key) - if (current != null) { - return current + if (this.containsKey(key)) { + return this.get(key) } else { val answer = defaultValue() this.put(key, answer)