Use of Inline Functions in Kotlin

  1. Kotlin inline Functions
  2. Use Kotlin inline Functions With Reified Type Parameters
  3. Use noinline in Kotlin inline Functions

Most Kotlin projects use high-order functions, which can be problematic for your Kotlin projects. Each high-order function is an object of a class and hence, needs closure.

It means that high-order functions require memory allocations every time they are called. Hence, every call of a high-order function leads to a runtime overhead.

Kotlin provides inline functions to overcome this issue. This article introduces the concept of Kotlin inline functions and when we can use them for better memory efficiency.

Kotlin inline Functions

Kotlin inline functions are declared using the keyword inline. We need to write the keyword right before declaring the function.

Using Kotlin inline functions enhances the memory efficiency of high-order functions. When we use inline functions, they tell the compiler to copy the function code to the call site, preventing memory allocation.

Here’s a basic example of the Kotlin inline function.

fun main(args: Array<String>) {
    exampleFunc({ println("This function will call our inline function")})
}

inline fun exampleFunc(Func: () -> Unit ) {
Func()
    print("This is an inline function call and doesn't require memory allocation")
}

Output:

Basic Kotlin inline Function Example

Run Code

When Not to Use inline Functions

While inline functions offer an excellent way to make higher functions more efficient, there are some declarations and scenarios that do not support their use, including:

  1. Local class declaration
  2. Inner nested class declaration
  3. Function expressions
  4. Local function declarations
  5. Optional parameters’ default value

Best Scenarios to Use inline Functions

The inline functions are not useful in every situation. Here are the best scenarios to use Kotlin inline functions:

  1. While accessing higher-order functions
  2. To enhance memory allocation efficiency
  3. When passing a functional type parameter
  4. To get better control flow
  5. When a function accepts another function as a parameter
  6. When a function accepts lambda as a parameter

Use Kotlin inline Functions With Reified Type Parameters

We can use Kotlin inline functions to access and retrieve the data type of the parameter passed during the call. We can do that using the reified modifier.

fun main(args: Array<String>) {
    exampleFunction<String>()
}

inline fun <reified S> exampleFunction() {
    print(S::class)
}

Output:

Reified Type Parameter in Kotlin inline Functions

Run Code

Use noinline in Kotlin inline Functions

When we declare a function inline, all its parameters become inline. However, if we want only some parameters to be inline, we can use the noinline modifier.

All the expressions in front of which we use the noinline modifier will be non-inlined.

Consider this example to understand the use of noinline in Kotlin inline functions.

fun main(args: Array<String>){
    println("Start")
        inlineFuncExample({ println("This is the first expression/parameter")
            return }, // This will not throw an error as it is an inline expression
        { println("This is the first expression/parameter")
            return } ) // This will throw an error as it is not an inline expression

    println("End")
}

inline fun inlineFuncExample( para1: () -> Unit, noinline para2: () -> Unit ) {
    para1()
    para2()
}

Output:

noinline Use in Kotlin inline Functions

Run Code

Related Article - Kotlin Function

  • Kotlin flatMap() Function
  • Use of Scoping Functions in Kotlin
  • Higher Order Functions in Kotlin