与协程一起使用时,地图上的 Kotlin foreach 与迭代键

Kotlin foreach on a map vs iterating over the keys when used with coroutines

我对以下代码的情况感到困惑。 task.yield 是从 a 到 b 的 hashmap,store.put 是一个带有 a 和 b 的挂起函数。遍历地图的第一种方法没有问题,第二种方法也是如此。第三种方式,对我来说是最自然的迭代方式,也是我最初编写的方式,导致 kotlin 抱怨挂起函数只能在协程体内调用。 我猜这与 forEaching 在地图上的工作方式有关(也许与列表相反?)但我真的不明白问题是什么。

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)
    }
}

编辑:我刚刚注意到列表 forEach 是一个内联函数,而我尝试使用的地图不是。我猜这就是问题所在。

事实上,接受 (K, V) -> UnitBiConsumer<? super K, ​? super V>)的 Map#forEach 的重载不是 Kotlin 标准库的一部分,而是 JDK 本身的一部分( Map#forEach)。这解释了为什么在此块中执行的任何内容都不是内联的,因此不是封闭的“挂起上下文”的一部分。

Kotlin 提供了一个非常相似的函数供您使用:

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

它接受一个 Entry<K, V>,所以你可以简单地 destructure 它在 lambda 中:

task.yield.forEach { (t, u) /* <-- this */ ->
    store.put(t, u)
}