Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

186
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!