noinline and crossinline Modifiers
Control inlining behavior for specific lambda parameters in inline functions.
The Problem inline Solves — And Introduces
When a function is inline, all its lambda parameters are inlined by default. Sometimes you need a specific lambda to not be inlined — for example to store it or pass it to another non-inline function.
noinline: Keeping One Lambda as an Object
Prefix a specific lambda parameter with noinline to opt it out of inlining while keeping the rest inlined.
inline fun doWork(
action: () -> Unit,
noinline callback: () -> Unit // stored, not inlined
) {
action() // inlined at call site
scheduleCallback(callback) // passed as object — valid
}