Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

233
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda