I'm confused about what is going on with the following code. task.yield is a hashmap from a to b, and store.put is a suspend function which takes an a and a b. The first way of iterating thru the map works with no issue, as does the second. The third way, which feels to me like the most natural way to do the iteration and was what I wrote initially, causes kotlin to complain that suspend functions can be called only within a coroutine body. I'm guessing this has to do with how forEaching on a map works (as opposed to a list maybe?) but I don't really understand what the issue is.
launch{
// Kotlin is perfectly happy with this
for(elt in task.yield.keys){
store.put(elt,task.yield[elt]!!)
}
// and this
task.yield.keys.forEach {
store.put(it,task.yield[it]!!)
}
// This makes kotlin sad. I'm not sure why
task.yield.forEach { t, u ->
store.put(t, u)
}
}
Edit: I've just noticed that the list forEach is an inline function while the map one I'm trying to use isn't. I'm guessing this is the issue.
Indeed, the overload of Map#forEach
that accepts a (K, V) -> Unit
(a BiConsumer<? super K, ? super V>
) is not part of the Kotlin standard libraries but rather of the JDK itself (Map#forEach
). This explains why anything executing within this block is not inlined and therefore not part of the enclosing "suspending context".
There is a very similar function that Kotlin provides that you can make use of:
inline fun <K, V> Map<out K, V>.forEach(action: (Entry<K, V>) -> Unit)
Performs the given action on each entry.
kotlin-stdlib / kotlin.collections / forEach
This accepts an Entry<K, V>
, so you can simply destructure it within the lambda:
task.yield.forEach { (t, u) /* <-- this */ ->
store.put(t, u)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With