Inline Function (Kotlin)
An inline function has its body (and any lambda arguments) copied directly into the call site by the compiler, eliminating the overhead of creating function objects for the lambdas.
Kotlin
Higher-order functions normally allocate an object for each lambda passed in. Marking a function inline tells the compiler to splice the function's code and its lambdas into the caller, removing those allocations. That's why standard library functions like map, filter, let, and apply are inline — they're called in hot loops constantly.
Inlining unlocks two extras: lambdas in an inline function can use non-local return (return from the enclosing function), and inline functions can have reified type parameters, letting you check or use a generic type at runtime (inline fun <reified T> isA(x: Any) = x is T).
Inlining copies code, so overusing it on large functions bloats the bytecode. Reserve inline for small higher-order functions; use noinline or crossinline to fine-tune individual lambda parameters when needed.
Syntax
inline fun <reified T> typeName(): String = T::class.simpleName ?: "?"Examples
Kotlin
inline fun <reified T> isInstance(x: Any): Boolean = x is T
println(isInstance<String>("hi"))
println(isInstance<Int>("hi"))Output:
true
false