getOrElse() and getOrPut() fixed to respect how maps work with nulls
This commit is contained in:
@@ -44,10 +44,9 @@ fun <K,V> Map.Entry<K,V>.component2() : V {
|
|||||||
*
|
*
|
||||||
* @includeFunctionBody ../../test/MapTest.kt getOrElse
|
* @includeFunctionBody ../../test/MapTest.kt getOrElse
|
||||||
*/
|
*/
|
||||||
public inline fun <K,V> Map<K,V>.getOrElse(key: K, defaultValue: ()-> V?) : V? {
|
public inline fun <K,V> Map<K,V>.getOrElse(key: K, defaultValue: ()-> V) : V {
|
||||||
val current = this.get(key)
|
if (this.containsKey(key)) {
|
||||||
if (current != null) {
|
return this.get(key)
|
||||||
return current
|
|
||||||
} else {
|
} else {
|
||||||
return defaultValue()
|
return defaultValue()
|
||||||
}
|
}
|
||||||
@@ -59,9 +58,8 @@ public inline fun <K,V> Map<K,V>.getOrElse(key: K, defaultValue: ()-> V?) : V? {
|
|||||||
* @includeFunctionBody ../../test/MapTest.kt getOrElse
|
* @includeFunctionBody ../../test/MapTest.kt getOrElse
|
||||||
*/
|
*/
|
||||||
public inline fun <K,V> MutableMap<K,V>.getOrPut(key: K, defaultValue: ()-> V) : V {
|
public inline fun <K,V> MutableMap<K,V>.getOrPut(key: K, defaultValue: ()-> V) : V {
|
||||||
val current = this.get(key)
|
if (this.containsKey(key)) {
|
||||||
if (current != null) {
|
return this.get(key)
|
||||||
return current
|
|
||||||
} else {
|
} else {
|
||||||
val answer = defaultValue()
|
val answer = defaultValue()
|
||||||
this.put(key, answer)
|
this.put(key, answer)
|
||||||
|
|||||||
Reference in New Issue
Block a user