# Kotlin Inline + Extension Function

> If get to know something new by reading my articles, don't forget to endorse me on [LinkedIn](https://www.linkedin.com/in/rommansabbir/)

### What is Kotlin Extension Functions?

Kotlin provides the ability to extend a class with new functionality without having to inherit from the class or use design patterns such as Decorator. This is done via special declarations called extensions.

For example, you can write new functions for a class from a third-party library that you can't modify. Such functions can be called in the usual way, as if they were methods of the original class. This mechanism is called an extension function. There are also extension properties that let you define new properties for existing classes.

### What is `Kotlin Inline Functions`:

An Inline function is a kind of function that is declared with the keyword **inline** just before the function declaration. Once a function is declared inline, the compiler does not allocate any memory for this function, instead the compiler copies the piece of code virtually at the calling place at runtime.

**Let's talk about our extension function called** `executeBodyOrReturnNull()`.

```plaintext
inline fun <T> executeBodyOrReturnNull(crossinline body: () -> T): T? {
    return try {
        body.invoke()
    } catch (e: Exception) {
        e.printStackTrace()
        null
    }
}
```

In every Android project, we have to deal with `Exception` by using `TryCatch` blocks. Writing too many `TryCatch` is a bad approach in terms of Modern Application Development using **Kotlin**. For this we can use an extension function that take a body as a parameter to **execute the given block** and if any exception occur, function print the `StackTrace` return a`null` object. Also, we are using the another superpower of `Kotlin` called `Inline functions`.

Let's take a look at the usages:

```plaintext
data class GenericData(val data: String)
```

We have a `Data Class` called `GenericData` which take `data : String` as a parameter. But in our app, we might need to cast the `data : String` to something else, like `Int` or `Double`.

If, we directly cast `data : String` into something else, program might throw `Exception` if something goes wrong. So, how can we solve this problem?

Simply, we can call our extension function `executeBodyOrReturnNull()`.

```plaintext
val firstObject = executeBodyOrReturnNull<Int> { GenericData("0").data.toInt() }
println(firstObject)

val secondObject = executeBodyOrReturnNull<Int> { GenericData("Hello").data.toInt() }
println(secondObject)
```

If, we run the code, the result would be:

```plaintext
0
null
```

and the `StackTrace` is

```plaintext
java.lang.NumberFormatException: For input string: "Hello"
	at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
	at java.base/java.lang.Integer.parseInt(Integer.java:668)
	at java.base/java.lang.Integer.parseInt(Integer.java:786)
	at GenericBodyExecutor$Companion.main(GenericBodyExecutor.kt:7)
	at GenericBodyExecutor.main(GenericBodyExecutor.kt)
```

Casting "0" into `Int` is successful, but casting "Hello" into `Int` will throw `NumberFormatException`, hence `executeBodyOrReturnNull()` returning `null` object.

Full implementation:

%[https://gist.github.com/rommansabbir/0c14f3e7a014d4578727662ef4f52fd9] 

Happy Coding...

Follow me on LinkedIn

%[https://www.linkedin.com/in/rommansabbir/]
