inline and crossinline in Kotlin

inline and crossinline in Kotlin

Kotlin’s powerful features often simplify code while enhancing performance. Among these, the inline and crossinline keywords play a crucial role when working with higher-order functions. While these keywords may seem intimidating at first, mastering them can lead to cleaner, more efficient code. In this article, we'll explore what inline and crossinline mean, when to use them, and demonstrate practical examples.


Understanding inline in Kotlin

The inline keyword is used in Kotlin to optimize higher-order functions by reducing overhead associated with lambda calls. When you mark a function as inline, the compiler inserts the function's code directly at the call site, eliminating the need for function calls.


Why use inline?

  • Performance Boost: Reduces overhead by avoiding function call costs for lambdas.
  • Access to Non-Local Returns: Allows returning from the enclosing function within the lambda.


Example

Article content
Example of inline function usage


What happens here?

  • Without inline, the lambda would be compiled into a separate class, introducing overhead.
  • With inline, the lambda’s code is directly embedded at the call site, improving performance.


Non-Local Returns with inline

One powerful feature of inline functions is their ability to support non-local returns, meaning you can return from the calling function inside the lambda.


Example

Article content
Example of non-local return usage


In this example:

  • The return inside the lambda causes the outer search() function (and even main()) to return early.
  • Without inline, this non-local return wouldn’t be possible.


Understanding crossinline in Kotlin

The crossinline keyword is used to restrict certain behavior in inline functions. Specifically, it prevents non-local returns in lambdas passed as parameters to inline functions.


Why use crossinline?

  • Ensures that the lambda doesn’t break the flow of the calling function by returning early.
  • Useful when you need an inline function but want stricter control over return behavior.


Example

Article content
Example of crossinline usage


Key difference:

  • crossinline blocks non-local returns, ensuring that the provided lambda can’t unexpectedly exit the surrounding function


Key Takeaways

  • Use inline for performance optimizations when working with higher-order functions.
  • Leverage inline to enable non-local returns where appropriate.
  • Apply crossinline when you need stricter control over the lambda’s return behavior, especially in async code.


By mastering these keywords, you can write safer, faster, and more efficient Kotlin code. Whether you're building Android apps with Jetpack Compose or developing backend services with Kotlin, understanding inline and crossinline is a powerful skill that can improve your code quality.

What are your favorite use cases for inline and crossinline? Share your insights in the comments!


To view or add a comment, sign in

More articles by Marek Tomczyk

Others also viewed

Explore content categories