Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

An inline function in Kotlin is a function declared with the inline modifier, which instructs the compiler to replace every call to the function with the actual bytecode of the function body and its lambda arguments. This mechanism eliminates the runtime overhead associated with higher-order functions, such as memory allocation for function objects (closures) and virtual method dispatch. When the Kotlin compiler encounters an inline function, it performs macro-like expansion at the call site.
inline fun execute(action: () -> Unit) {
    println("Start")
    action()
    println("End")
}

fun main() {
    execute {
        println("Executing")
    }
}
During compilation, the compiler transforms the main function above into the following equivalent bytecode representation, completely bypassing the creation of a Function0 instance:
fun main() {
    println("Start")
    println("Executing")
    println("End")
}

The noinline Modifier

By default, if an inline function accepts multiple lambda parameters, all of them are inlined. However, if a lambda parameter needs to be manipulated as a standard object—such as being stored in a variable or passed to a non-inline function—it must be marked with the noinline modifier. This instructs the compiler to generate a standard function object for that specific parameter while still inlining the rest of the function.
inline fun process(
    inlinedAction: () -> Unit, 
    noinline retainedAction: () -> Unit
) {
    inlinedAction() // Bytecode is substituted
    val reference = retainedAction // Valid: retainedAction is a standard object
}

Non-Local Returns and crossinline

Because inlined lambdas are substituted directly into the call site, they allow for non-local returns. A return statement inside an inlined lambda will return from the enclosing function, not just the lambda itself. If the inline function passes the lambda to a different execution context (such as a local object, a nested function, or another thread), non-local returns violate control flow validity. To prevent the caller from using a non-local return, the lambda parameter must be marked with crossinline.
inline fun executeAsync(crossinline action: () -> Unit) {
    val runnable = object : Runnable {
        override fun run() {
            action() // Valid, but non-local returns are forbidden inside the lambda
        }
    }
    Thread(runnable).start()
}

Reified Type Parameters

Standard generic types in Kotlin are subject to type erasure at runtime. Because inline functions substitute bytecode directly at the call site, the compiler has access to the exact types used during the invocation. By combining the inline keyword with the reified modifier on a generic type parameter, the type information is preserved in the generated bytecode, allowing runtime type checks (is, as, or T::class.java).
inline fun <reified T> checkType(value: Any): Boolean {
    return value is T // T is statically resolved at the call site
}

Inline Properties

The inline modifier can also be applied to properties or their specific accessors (getters and setters), provided the property does not have a backing field. The compiler will inline the accessor’s bytecode at every read or write site.
val isReady: Boolean
    inline get() = true // The getter bytecode is inlined at the call site

inline var status: Int
    get() = 1
    set(value) { println(value) }
Master Kotlin with Deep Grasping Methodology!Learn More