Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

151
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda