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

189
Visualizações
How to annotate lambda's return value

I have a function with lambda parameter:

private fun MyFun(
    progress: () -> Float,
) {
    // ...
}

I want to annotate return value of progress lambda with @FloatRange(from = 0.0, to = 1.0), but can't figure out how to do this. All my attempts to solve this causes syntax errors. Where am I wrong?

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

0

You can define a functional interface to describe your type, and annotate it there:

fun interface ProgressCallback {
    @FloatRange(from = 0.0, to = 1.0) fun progress(): Float
}

private fun myFun(progress: ProgressCallback) {
    // ...
}

fun foo() {
    myFun { 5.0f } // error, doesn't conform to FloatRange
}

FYI, lint is not sophisticated enough to detect a failure to meet the requirement if you pass a function reference instead of a lambda, or if you indirectly call a function that returns a Float:

fun bar() = 5.0f

fun foo() {
    myFun(::bar) // no error
    myFun { bar() } // no error
}

Or if you return anything besides a single literal value:

fun foo() {
    myFun { 1.0f * 2.0f } // no error
}
over 4 years ago · Santiago Trujillo Relatório

0

In kotlin language, lambda to perform a task, not to annotate. You can comment code likes this // FloatRange(from = 0.0, to = 1.0)

example :

// FloatRange(from = 0.0, to = 1.0)

private fun MyFun(

progress: () -> Float,

) {

}

you can read here to better understand lambda in kotlin language

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