How to Use of Inline Functions in Kotlin

Kailash Vaviya Feb 15, 2024
  1. Kotlin inline Functions
  2. Use Kotlin inline Functions With Reified Type Parameters
  3. Use noinline in Kotlin inline Functions
How to Use of Inline Functions in Kotlin

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

Kailash Vaviya avatar Kailash Vaviya avatar

Kailash Vaviya is a freelance writer who started writing in 2019 and has never stopped since then as he fell in love with it. He has a soft corner for technology and likes to read, learn, and write about it. His content is focused on providing information to help build a brand presence and gain engagement.

LinkedIn

Related Article - Kotlin Function