Currently it's implemented as ``` public inline fu...
# getting-started
r
Currently it's implemented as
Copy code
public inline fun <K, V> MutableMap<K, V>.getOrPut(key: K, defaultValue: () -> V): V {
    val value = get(key)
    return if (value == null) {
        val answer = defaultValue()
        put(key, answer)
        answer
    } else {
        value
    }
}
but it seems to me (based on the kdoc) that is should be more like
Copy code
public inline fun <K, V> MutableMap<K, V>.getOrPut(key: K, defaultValue: () -> V): V {
    return if (contains(key)) {
        getValue(key)
    } else {
        val answer = defaultValue()
        put(key, answer)
        answer
    }
}
d
I guess it would be correct if
V
were constrained to
Any
(
<K, V : Any>
). So yeah, I would say that you're correct
r
Good to know I'm not completely crazy yet. Maybe I'll file an issue and see what becomes of it.
i
Originally it had the second implementation, but then we've changed it to the first to be consistent with JDK methods like
compute
etc, that do not distinguish
null
and missing value.
r
🕵️‍♂️ 1