Member-only story

Do you know the exact use of Kotlin’s inline and no inline modifiers?

Android development with Kotlin
2 min readJan 19, 2025

Inline and non-inline modifiers work with higher-order functions in Kotlin. Its primary role is to optimize performance and manage lambdas. Come, let us understand the difference between these two.

Inline Function
When a function is inline, its body is replaced directly at the call site.
This results in reduced overhead of function calls, which is beneficial for short and frequently called functions.
If lambda expressions are passed with an inline function, their object is not allocated, hence memory usage becomes efficient.
Example:

inline fun performAction(action: () -> Unit) {
action() // Lambda body will be directly replaced here.
}

fun main() {
performAction({ println("Hello Kotlin!") })
}

Advantages of using inline function:

  1. Performance: Function calls are faster because there is no extra overhead.
  2. Memory Saving: Lambda does not need to create a separate object.

No inline Function
If lambda is marked as no inline, its object is created and is not inlined.
noinline is used when it is not necessary to inline the lambdas or they have to be passed outside the function.
Example:

inline fun executeTasks(primaryTask: () -> Unit, noinline secondaryTask: ()…

--

--

No responses yet