Sobes.tech
Junior — Middle

Can you name the main keywords used when applying `inline` in code?

sobes.tech AI

Answer from AI

In Android (Kotlin), the keyword inline is used for functions to have the compiler insert the function body directly at the call sites, reducing call overhead and potentially improving performance.

Main keywords related to inline:

  • inline — declares a function as inline.
  • noinline — used for function parameters to prevent them from being inlined inside an inline function.
  • crossinline — prohibits the use of return from a lambda passed into an inline function, preventing incorrect exits from the outer function.

Example:

inline fun perform(action: () -> Unit) {
    println("Before action")
    action() // the lambda body will be inserted here
    println("After action")
}

fun main() {
    perform {
        println("Action is being performed")
    }
}

This allows for code optimization, especially when working with lambdas.