Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

223
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!