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

301
Visualizações
Why does ide say "Value captured in a closure" when using Iterable.flatMap but doesn't when using map then flatten in kotlin?
inline fun <T : Any, E : Any> batchQuery(
    paramsToBeChunked: Collection<T>,
    batchSize: Int = 500,
    crossinline queryAction: (params: Collection<T>) -> List<E>
) = paramsToBeChunked.chunked(batchSize).flatMap { queryAction(it) } 

enter image description here

inline fun <T : Any, E : Any> batchQuery(
    paramsToBeChunked: Collection<T>,
    batchSize: Int = 500,
    crossinline queryAction: (params: Collection<T>) -> List<E>
) = paramsToBeChunked.chunked(batchSize).map { queryAction(it) }.flatten()

enter image description here

Is there any potential problem when using flatMap?

IMHO, map, flatten, flatMap are all inline functions, so these two implementations are equivalent, is this right?

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

0

If the fun wasn't inline then in both cases queryAction would have to be captured in a closure, because it's coming from outside of the lambda. It means that a class which is generated under the hood holds a reference to the variable, and when the lambda is eventually executed, it uses that reference.

But here, it's inlined just fine. Idea tries it's best to give information about captured variables but in this situation it might be a limitation of Idea's static analysis. The first message is just incorrect.

As @broot suggested, you can replace .map { f(it) } with just .map(f).

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