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

155
Visualizações
Why is Result.map in Kotlin behaving diferently for Unit types?

I am confused about the following two Kotlin code snippets:

A:

val r: Result<Unit> = Result.success(Unit).map { Result.success(Unit) }

B:

val r: Result<Int> = Result.success(Unit).map { Result.success(1) }

Snippet A compiles while snippet B does not compile.

Can someone explain why snippet A works with the Kotlin type system.

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

0

You're not supposed to return a Result from the map lambda. You're supposed to return a value that will be wrapped in a Result. That's why your code B fails. It would succeed with just map { 1 }.

Code A succeeds because when Kotlin expects a lambda to return Unit, it implicitly returns Unit regardless of what the last line of code in the lambda is. If it didn't do this, any lambda that has no return value would have to end with a line that says Unit. In the same way, any function with no explicit return value has an implicit return type of Unit, but the compiler does not require you to explicitly write return Unit at the end of your functions.

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