noinline in Kotlin
Tags: android, kotlin
Published: 2023-01-17 05:40:57

---
title: noinline in Kotlin
published: true
description: In this blog, we will learn about the noinline modifier in Kotlin.
tags: android, kotlin
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vy6bkl63or3ljib078kk.png
canonical_url: https://amitshekhar.me/blog/noinline-in-kotlin
---

I am [**Amit Shekhar**](https://amitshekhar.me), a mentor helping developers in getting high-paying tech jobs.

Before we start, I would like to mention that, I have released a video playlist to help you crack the Android Interview: Check out [Android Interview Questions and Answers](https://www.youtube.com/playlist?list=PL_I3TGB7aK6jNBMZkw3FYdJXyf7quHdI8).

In this blog, we will learn about the `noinline` modifier in Kotlin.

**This article was originally published at [amitshekhar.me](https://amitshekhar.me/blog/noinline-in-kotlin).**

What is a `noinline` in Kotlin?

Assume that we do not want all of the lambdas passed to an `inline` function to be inlined, in that case, we can mark those function parameters with the `noinline` modifier.

As we are learning about the `noinline`, we must be knowing about the `inline` keyword in Kotlin. You can learn [here](https://amitshekhar.me/blog/inline-function-in-kotlin).

Let's understand this `noinline` modifier with an example.

```kotlin
fun guide() {
    print("guide start")
    teach({
        print("teach abc")
    }, {
        print("teach xyz")
    })
    print("guide end")
}

inline fun teach(abc: () -> Unit, xyz: () -> Unit) {
    abc()
    xyz()
}
```

Let's go to the decompiled code. The decompiled code is as below:

```java
public void guide() {
    System.out.print("guide start");
    System.out.print("teach abc");
    System.out.print("teach xyz");
    System.out.print("guide end");
}
```

Here, both the lambdas(`abc`, `xyz`) passed to an inline function got inlined.

What if we want only the first lambda(`abc`) to get inlined but not the second lambda(`xyz`)?

In that case, we will have to use the `noinline` modifier with that lambda(`xyz`).

Our updated code:

```kotlin
fun guide() {
    print("guide start")
    teach({
        print("teach abc")
    }, {
        print("teach xyz")
    })
    print("guide end")
}

inline fun teach(abc: () -> Unit, noinline xyz: () -> Unit) {
    abc()
    xyz()
}
```

Again, let's go to the decompiled code. The decompiled code is as below:

```java 
public void guide() {
    System.out.print("guide start");
    System.out.print("teach abc");
    teach(new Function() {
        @Override
        public void invoke() {
            System.out.print("teach xyz");
        }
    });
    System.out.print("guide end");
}

public void teach(Function xyz) {
    xyz.invoke();
}
```

Here, we can see that the lambda(`xyz`) has not got inlined as expected. Only the lambda(`abc`) got inlined. This is how `noinline` in Kotlin helped us in achieving what we wanted.

This way, we used the `noinline` to avoid the inlining.

Now, we have understood the `noinline` in Kotlin.

Learn about crossinline: [crossinline in Kotlin](https://amitshekhar.me/blog/crossinline-in-kotlin)

Master Kotlin Coroutines from here: [Mastering Kotlin Coroutines](https://amitshekhar.me/blog/kotlin-coroutines)

That's it for now.

Thanks

[**Amit Shekhar**](https://amitshekhar.me)

You can connect with me on:

- [Twitter](https://twitter.com/amitiitbhu)
- [YouTube](https://www.youtube.com/@amitshekhar)
- [LinkedIn](https://www.linkedin.com/in/amit-shekhar-iitbhu)
- [GitHub](https://github.com/amitshekhariitbhu)

[**Read all of my high-quality blogs here.**](https://amitshekhar.me/blog)