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

253
Visualizações
Converting java.util.function.Function to Kotlin's functional interface type

I am working with two modules that are coded in Java and Kotlin respectively, hence require some bit of interoperability.

In one of the cases the java module returns functional interfaces with the type java.util.function.Function, which is to be consumed by the kotlin code.

I could use the java type as it is. However, I would like to be able to cast this to kotlin's native type, be it kotlin.Function or kotlin.reflect.KFunction. The reason behind doing so is to be able to support a similar kotlin module as that of the java one, which surfaces functions for the client.

Is there any way to do this?

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

0

You can't cast between them, they are just two different interfaces. But it's trivial to write conversion functions:

import java.util.function.Function

fun <A, B> Function<A, B>.toKotlin(): (A) -> B = { this.apply(it) }

fun <A, B> ((A) -> B).toJava(): Function<A, B> = Function { this(it) }

kotlin-stdlib-jdk8 could provide these, but doesn't seem to.

And of course, you can just use both Kotlin and Java lambdas/method references for both in most circumstances.

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