SecretX
12/22/2021, 8:41 PMConcurrentHashMap<Int, String>().getOrPut(1) { null } // compiles just fine, but it'll throw in runtime
mutableMapOf<Int, String>().getOrPut(1) { null } // compiler error: Null can not be a value of a non-null type String
SecretX
12/22/2021, 8:50 PMBig Chungus
12/22/2021, 9:35 PMSecretX
12/22/2021, 11:40 PMgetOrPut
is a Kotlin extension function of stdlib
, not a method of ConcurrentMap
interface, so that makes no sense.
Here is its signature.
public inline fun <K, V> java.util.concurrent.ConcurrentMap<K, V>.getOrPut(key: K, defaultValue: () -> V): V { /* compiled code */ }
Paul Griffith
12/23/2021, 12:25 AMRichard Gomez
12/23/2021, 4:44 AMinline fun <K, V> ConcurrentMap<K, V>.getOrPut(key: K, defaultValue: () -> V): V
inline fun <K, V> MutableMap<K, V>.getOrPut(key: K, defaultValue: () -> V): V
https://github.com/JetBrains/kotlin/blob/1e378ed136e19d3c2d0026a8382fe5d2d24404a9/libraries/stdlib/jvm/src/kotlin/collections/MapsJVM.kt#L71
https://github.com/JetBrains/kotlin/blob/master/libraries/stdlib/src/kotlin/collections/Maps.kt#L356Richard Gomez
12/23/2021, 4:50 AMV
from ConcurrentMap implicitly Any!
whereas it's Any
for MutableMap.Richard Gomez
12/24/2021, 6:32 PMAny?
, but I wanted to start the ball rolling.