Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

235
Visualizações
Kotlin foreach on a map vs iterating over the keys when used with coroutines

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.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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)
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda