noinline and crossinline Modifiers
Control inlining behavior for specific lambda parameters in inline functions.
noinline and crossinline Modifiers is a free Kotlin Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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
}When noinline Is Necessary
You must use noinline when the lambda is:
- Stored in a variable or field
- Passed to another function that expects a function type
- Returned from the inline function
crossinline: Forbidding Non-Local Returns
crossinline is used when an inlined lambda will be invoked in a different execution context (e.g., inside another lambda or a different thread). It tells the compiler: inline this lambda but do not allow non-local returns inside it.
inline fun runAsync(crossinline block: () -> Unit) {
Thread {
block() // block is inlined but cannot use non-local return
}.start()
}Why Non-Local Returns Are Dangerous Here
If block could use a non-local return, it would try to return from the function that called runAsync. But by the time the thread runs, that function may already have returned — causing undefined behavior. crossinline prevents this at compile time.
Combining noinline and crossinline
A single inline function can mix regular, noinline, and crossinline parameters:
inline fun orchestrate(
crossinline setup: () -> Unit,
action: () -> Unit,
noinline teardown: () -> Unit
) {
Thread { setup() }.start()
action()
register(teardown)
}Compiler Error Without crossinline
If you pass an inlined lambda to a lambda context without crossinline, the compiler emits: "Can't inline 'block' here: it may contain non-local returns. Add crossinline modifier."
Standard Library Use
The standard library uses crossinline in functions like Sequence { } and some coroutine builders, ensuring that the user-supplied lambda runs safely in a different context.
noinline Does Not Break inline
Marking one parameter noinline does not turn off inlining for the whole function. The function body and all other lambda parameters are still inlined. Only the noinline-marked lambda retains its object identity.
Performance Considerations
noinline incurs the usual lambda allocation cost for that one parameter. crossinline still inlines — the only restriction is disallowing non-local returns. Neither modifier affects the inlining of other parameters.
Summary Table
Modifier summary:
- No modifier → lambda fully inlined, non-local returns allowed
noinline→ lambda kept as object, can be stored/passedcrossinline→ lambda inlined, non-local returns forbidden
Quick Check
When must you use noinline on a lambda parameter of an inline function?
Recap: noinline and crossinline
Key takeaways:
noinline: keeps one lambda as an object inside an otherwise-inline functioncrossinline: inlines the lambda but bans non-local returns, for safe cross-context use- Both modifiers apply per-parameter, not to the whole function
Frequently asked questions
Is the “noinline and crossinline Modifiers” lesson free?
Yes — the full text of “noinline and crossinline Modifiers” is free to read here on the web, and the Kotlin Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Academy course, upgrade to CoddyKit PRO.
What will I learn in “noinline and crossinline Modifiers”?
Control inlining behavior for specific lambda parameters in inline functions. You practise Kotlin Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Kotlin Academy?
No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “noinline and crossinline Modifiers” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Kotlin Academy lesson?
Yes. Every Kotlin Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.