Kotlin: Inline function
Tags: kotlin, inline
Published: 2020-05-04 10:41:51

#Prerequisite/Previous articles:
1. [Kotlin: Function type, higher order function, function literal, lambda expression and an anonymous function](https://dev.to/srdpatel/kotlin-function-type-function-literal-lambda-expression-and-anonymous-function-lbd)

2. [Kotlin: Extension function and Receiver type](https://dev.to/srdpatel/kotlin-extension-function-1dd7)

#Inline function

We [know](https://dev.to/srdpatel/kotlin-function-type-function-literal-lambda-expression-and-anonymous-function-lbd) that a new function object is created for each and every function type in a higher order function. It causes memory allocation and hence, runtime overhead. To avoid this or say, to improve the performance, we can use the keyword **inline**.

However, that doesn’t mean we should inline each and every function! In fact, when we try to _inline_ a function that is not accepting a function type, the IDE will tell us to remove the _inline_ keyword.

Also, we should not _inline_ any higher order function that has more than 3 lines because generated code grows a lot for an _inline function_. You can check the Kotlin standard library to get an idea about when to use inline functions.

When using an inline function, we are not allowed to pass a function type parameter to another function. However, if it is necessary, we can mark such a function type as a **noinline**.

Let us understand it by an example:

Suppose we have our higher order function like below:

```kotlin
fun Int.doSomething(y: Int, ftOne: Int.(Int) -> Int, ftTwo: (Int) -> Int) {
        
           /*...*/
}
```
So, in such case, in order to prevent new function object creation, we can make the function inline as below:

```kotlin
inline fun Int.doSomething(y: Int, ftOne: Int.(Int) -> Int, ftTwo: (Int) -> Int) {
        /*...*/
}
```
However, if we are passing any function type to another function like below:

```kotlin
//Compile time error… Illegal usage of inline function type ftOne...
inline fun Int.doSomething(y: Int, ftOne: Int.(Int) -> Int, ftTwo: (Int) -> Int) {
        //passing a function type to another function
        val funOne = someFunction(ftOne)
        /*...*/
}
```
Here, the compiler will complain to us: Illegal usage of inline function type ftOne…

To solve that, we can rewrite our function as below:

```kotlin
inline fun Int.doSomething(y: Int, noinline ftOne: Int.(Int) -> Int, ftTwo: (Int) -> Int) {
        //passing a function type to another function
        val funOne = someFunction(ftOne)
        /*...*/
}
```
However, if there is only one lambda parameter and we are passing it to another function, it doesn’t make any sense to make such a higher order function inline and the compiler will also suggest the same!

Let us understand it by an example:

Suppose we have a higher order function like below:

```kotlin
inline fun Int.doSomething(y: Int, noinline ftOne: Int.(Int) -> Int) {
        //passing a function type to another function
        val funOne = someFunction(ftOne)
        /*...*/
}
```
Here, the compiler will tell us to not use the inline keyword when there is only one lambda parameter and we are passing it to another function. So, we can rewrite above function as below:

```kotlin
    fun Int.doSomething(y: Int, ftOne: Int.(Int) -> Int) {
        //passing a function type to another function
        val funOne = someFunction(ftOne)
        /*...*/
    }
```
Note that we had to remove the keyword _noinline_ as well because it can be used only for inline functions!

That’s all!

If you have read this article, if this article has helped you, you can click on that ❤ icon. Your ❤ motivates me to write more articles.

You can follow me to get the notification when I publish a new article. 

**Let us be Connected**

https://www.linkedin.com/in/srdpatel

https://twitter.com/iSrdPatel

Tags: kotlin, inline