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

173
Views
Why we need noinline in kotlin?

I'm reading kotlin doc and can't understend one thing.

We can use noinline modifier to mark that lambda shouldn't be inline. But what is the real case when we really need it?

I understend what noinline does, but can't understand why.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Notice that when inline is added to a function/method declaration, all of its lambda parameters are inlined.

Also notice that there are some ways in which you can use a lambda parameter, that requires the lambda to be not inlined. For example, passing it to another, non-inlined function, or assigning it to a variable:

var func: (() -> Unit)? = null
inline fun foo(x: () -> Unit, y: () -> Unit) {
    notInlined(x) // x must be not inlined for this to work
    func = x // x must be not inlined for this to work
}

So what if I want to do notInlined(x), but I still want to inline y? This is where I use noinline:

inline fun foo(noinline x: () -> Unit, y: () -> Unit)

IntelliJ even gives this as a suggestion:

enter image description here

over 4 years ago · Santiago Trujillo Report

0

One thing I notice is that this for example is allowed, but "end of runLambda" is never reached then.

fun main() {
    runLambda {
        println("lambda")
        return
    }

}

inline fun runLambda(lambda: ()->Unit) {
    lambda.invoke()
    println("end of runLambda")
}

if you want to ensure that the function completely runs until the end noinline could fix it because then it won't allow the return

enter image description here

Although crossinline can do the same and I think that one is actually meant for that. I'm not sure what the difference is

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!