crossinline in Kotlin
Tags: kotlin, android
Published: 2023-01-20 06:25:14

---
title: crossinline in Kotlin
published: true
description: In this blog, we will learn about the crossinline modifier in Kotlin.
tags: kotlin, android
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q5me8wcjm0qx92ap38bp.png
canonical_url: https://amitshekhar.me/blog/crossinline-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 `crossinline` modifier in Kotlin.

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

What is a `crossinline` in Kotlin?

`crossinline` in Kotlin is used to avoid non-local returns.

Do not worry about the term "non-local returns", we will learn it with an example.

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

Let's take an example to understand the non-local returns.

```kotlin 
fun guide() {
    print("guide start")
    teach {
        print("teach")
        return
    }
    print("guide end")
}

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

Here, we have added a return statement in the lambda function that we are passing.

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");
}
```

When we notice the decompiled code, we find that there is no `System.out.print("guide end")`. As we have added the **return** inside the lambda, it allowed the **non-local returns** and left the code below that.

Now, we have an idea of non-local returns.

How can we avoid this situation?

`crossinline` in Kotlin is the solution to avoid non-local returns.

**When we add the `crossinline`, then it will not allow us the put the return inside that lambda.**

Let's use the `crossinline`.

Our updated code with `crossinline`:

```kotlin 
fun guide() {
    print("guide start")
    teach {
        print("teach")
        // return is not allowed here
    }
    print("guide end")
}

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

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");
    System.out.print("guide end");
}
```

Now, we can see that everything is as expected. We also have the `System.out.print("guide end")`.

This is how the `crossinline` can help us to avoid the "non-local returns".

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

Learn about noinline: [noinline in Kotlin](https://amitshekhar.me/blog/noinline-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)