Questions
KotlinIntermediate4 min
What is inline function in Kotlin?
Recommended Resources
Answer
An inline function instructs the compiler to copy the function's code and paste it directly into the call site, instead of creating a standard function call object.
Why use it?
- Performance: Reduces overhead of function calls and object allocation, especially for Higher-Order Functions (functions that take lambdas).
- Without `inline`, every lambda passed to a function creates an anonymous class object in Java bytecode, which costs memory.
Example
```kotlin inline fun executeOp(op: () -> Unit) { println("Start") op() println("End") }
// Usage executeOp { println("Working") } ```
Compiler output (conceptually): ```kotlin println("Start") println("Working") // The lambda body is pasted here println("End") ```
Caveat
Don't inline large functions, as it increases the bytecode size of your application.
1:1 Mentorship
Get personalized guidance from a Google Developer Expert. Accelerate your career with dedicated support.
Personalized Learning Path
Mock Interviews & Feedback
Resume & Career Guidance
Share & Help Others
Help fellow developers prepare for interviews
Sharing helps the Android community grow 💚