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

227
Visualizações
Kotlin supplyAsync with executor

I want to create a CompletableFuture with a return value that runs on a specific executor in Kotlin.

The following code works just fine.

return CompletableFuture.supplyAsync {
        val commandHandler = registry.get<TCommand, TResponse>(command::class.java)
        commandHandler.handle(command)
 }

However when I attempt to pass the executor it won't compile.

return CompletableFuture.supplyAsync({
        val commandHandler = registry.get<TCommand, TResponse>(command::class.java)
        commandHandler.handle(command)
}, exec)

I tried to get clever and wrote the Java version and had Intellij covert it to Kotlin, but that one had the same error as well. What am I doing wrong here?

enter image description here

EDIT:

I can make it work by doing the following but it seems unnecessary. Can someone explain why this works but other methods do not. Are there other ways to write this code?

return CompletableFuture.supplyAsync(Supplier {
    commandHandler.handle(command)
}, exec) 
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I do not exactly know, why it is not working. But the following does work:

return CompletableFuture.supplyAsync({
    val commandHandler = registry.get<TCommand, TResponse>(command::class.java)
    commandHandler.handle(command)
}, exec::execute)

As you can see, I changed the second parameter to a method reference. Now the signature of the method is:

supplyAsync(supplier: () -> U, executor: (Runnable)-> Unit)

If you pass the Executor directly, Kotlin chooses the signature:

supplyAsync(supplier: Supplier<U>, executor: Executor)

It looks like you can not mix interface and lambda style.

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