WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Kotlin · Intermediate · question 40 of 100

What is the purpose of the noinline keyword in Kotlin, and when should you use it?

📕 Buy this interview preparation book: 100 Kotlin questions & answers — PDF + EPUB for $5

In Kotlin, the ‘noinline‘ keyword is used to indicate that a lambda or function parameter with a function type should not be inlined by the compiler. By default, when a lambda or function is passed as an argument to another function, the compiler may choose to inline it for performance reasons. This can lead to better performance by reducing the overhead of function calls, but it can also have unintended consequences, such as increasing the size of the compiled code or affecting the behavior of code that relies on reflection or dynamic dispatch.

The ‘noinline‘ keyword can be used to prevent the compiler from inlining a lambda or function parameter. When a lambda or function parameter is marked with ‘noinline‘, the compiler will always generate a separate function for it, instead of inlining it at the call site. This can be useful in situations where the inlining behavior of the compiler is not desirable, such as when the lambda or function parameter needs to be passed to a higher-order function that expects a function reference rather than an inlined lambda.

Here’s an example of using the ‘noinline‘ keyword to prevent inlining of a lambda:

fun <T> List<T>.forEachIndexedNonInlined(action: (Int, T) -> Unit) {
    for (i in indices) {
        action(i, this[i])
    }
}

fun main() {
    val list = listOf("foo", "bar", "baz")
    list.forEachIndexedNonInlined { i, value ->
        println("Item $i is $value")
    }
}

In this example, we define a new function ‘forEachIndexedNonInlined‘ that takes a lambda parameter ‘action‘ and iterates over the elements of a list, calling ‘action‘ with the index and value of each element. We use the ‘noinline‘ keyword to prevent the lambda from being inlined by the compiler, which ensures that the lambda is compiled as a separate function and can be passed to ‘forEachIndexedNonInlined‘ as a reference.

In general, you should use the ‘noinline‘ keyword when you want to ensure that a lambda or function parameter is compiled as a separate function, rather than being inlined by the compiler. This can be useful in situations where you want to pass the lambda or function parameter to a higher-order function that expects a function reference, or when you want to avoid the potential side effects of inlining, such as increased code size or differences in behavior due to reflection or dynamic dispatch.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Kotlin interview — then scores it.
📞 Practice Kotlin — free 15 min
📕 Buy this interview preparation book: 100 Kotlin questions & answers — PDF + EPUB for $5

All 100 Kotlin questions · All topics