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

123
Visualizações
Kotlin - Infer type for one of two generic parameters

I am trying to create a function that has two generic types: one reified, and another derived from the context of its usage (since it is an extension function):

inline fun <reified E, A> Either<Throwable, A>.bypassLeft(transformation: Throwable.() -> A): Either<Throwable, A> =
    when (this) {
        is Either.Left -> when (value) {
            is E -> value.transformation().right()
            else -> this
        }
        else -> this
    }

The idea would be to call the function just mentioning the reified type, something like:

a.bypassLeft<NoResultException> { "" }

In which "a" is an object of type Either<Throwable,String>

But the compiler is not letting me go away with it, and requires me to specify both generic types, instead of deriving the second one form the object calling the function. It seemed quite a reasonable thing to be possible, but maybe I am wrong...

Is this possible to achieve? If so, what am I doing wrong?

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

0

It's not currently possible with a function to ascribe a single type argument and leave the other inferred. You can achieve what you want if you type the lambda arguments by changing your implementation to not use a receiver type.

I threw in there an additional impl that shows how type args can also be partially applied with a class or other surrounding scope.

import arrow.core.Either
import arrow.core.right

inline fun <reified E : Throwable, A> Either<Throwable, A>.bypassLeft(
  transformation: (E) -> A //changed to regular arg not receiver
): Either<Throwable, A> =
  when (this) {
    is Either.Left -> when (val v = value) { //name locally for smart cast
      is E -> transformation(v).right()
      else -> this
    }
    else -> this
  }

class Catch<A>(val f: () -> A) { //alternative impl with partial type app
  inline fun <reified E : Throwable> recover(
    recover: (E) -> A
  ): Either<Throwable, A> =
    Either.catch(f).fold(
      {
        if (it is E) Either.Right(recover(it))
        else Either.Left(it)
      },
      {
        Either.Right(it)
      }
    )
}

suspend fun main() {

  val x: Either<Throwable, Int> = Either.Left(StackOverflowError())
  val recovered = x.bypassLeft { 
     s: StackOverflowError -> //here infers E
     0 // here infers A
  }

  println(recovered) // Either.Right(0)

  val notRecovered: Either<Throwable, Int> =
    Catch {
      throw NumberFormatException()
      1
    }.recover<StackOverflowError> { 0 }

  println(notRecovered) // Either.Left(java.lang.NumberFormatException)

}
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