Kotlin Inline Functions: What, Why, and When to Use
Kotlin introduces a powerful feature called inline functions, mainly to optimize performance when working with higher-order functions…

Kotlin Inline Functions: What, Why, and When to Use
Kotlin introduces a powerful feature called inline functions, mainly to optimize performance when working with higher-order functions (i.e., functions that take other functions as parameters).
Non Members can visit this link
What Are Inline Functions?
An inline function is a function where the compiler copies the function’s code directly into the place where it’s called, instead of generating a function call at runtime.
This is particularly useful when working with lambda functions (i.e., functions passed as parameters), which normally involve extra allocations and function calls.
- Eliminating lambda object creation.
- Avoiding function call overhead.
Here’s a simple example:
inline fun measureTime(block: () -> Unit) {
val start = System.currentTimeMillis()
block()
println("Took ${System.currentTimeMillis() - start} ms")
}
What happens without inline?
When you call:
measureTime {
doHeavyWork()
}
The Kotlin compiler generates a function object for the lambda ({ doHeavyWork() }) and passes it to measureTime at runtime, resulting in:
- A function call to
measureTime - Allocation of a lambda object (
block) - A call to
block()insidemeasureTime
This adds overhead, especially in performance-critical paths.
What happens with inline?
With the inline keyword, the compiler replaces the call at compile time by inserting the full body of measureTime at the call site. So it becomes something like:
val start = System.currentTimeMillis()
doHeavyWork()
println("Took ${System.currentTimeMillis() - start} ms")
This way:
- There’s no function call to
measureTime - There’s no lambda object created
- Everything is executed in-place, reducing runtime cost
Here’s another example:
- Allowing access to non-local returns (
returninside lambda returns from the outer function).
Non-Local Returns in Inline Functions
Kotlin’s inline functions allow non-local returns — meaning you can return directly from the calling function inside a lambda, as long as the lambda is passed to an inline function.
This is not possible with regular (non-inline) functions.
Without inline – This won't compile:
fun doSomething(block: () -> Unit) {
block()
}
fun test() {
doSomething {
return // ❌ Error: Cannot return from outer function here
}
println("This will not be reached")
}
You’ll get a compiler error, because the lambda can’t return from the surrounding test() function.
With inline – Works perfectly:
inline fun doSomething(block: () -> Unit) {
block()
}
fun test() {
doSomething {
return // ✅ This will return from 'test', not just from the lambda
}
println("This will never be printed")
}
What happens?
- Since
doSomethingis inline, the lambdablockis also inlined at the call site. - This makes the
returninside the lambda behave like it's part of the outer function. - So
returnhere exits fromtest(), not just fromblock.
Kotlin’s inline functions are not just performance hacks — they’re essential tools for writing expressive, concise, and efficient higher-order APIs.
메타데이터
- post_id
- 3dce45fef2ab
- slug
- kotlin-inline-functions-what-why-and-when-to-use-3dce45fef2ab
- url
- https://medium.com/@sidcasm/kotlin-inline-functions-what-why-and-when-to-use-3dce45fef2ab
- canonical_url
- https://medium.com/@sidcasm/kotlin-inline-functions-what-why-and-when-to-use-3dce45fef2ab
- author_url
- https://medium.com/@sidcasm
- status
- ok
- fetched_at
- 2026-08-06 07:48:24