Kotlin Inline + Extension Function
GenericBodyExecutor to execute a given body in Kotlin by handling exception.
Table of contents
If get to know something new by reading my articles, don't forget to endorse me on LinkedIn
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()
.
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 anull
object. Also, we are using the another superpower of Kotlin
called Inline functions
.
Let's take a look at the usages:
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()
.
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:
0
null
and the StackTrace
is
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:
Happy Coding...
Follow me on LinkedIn