Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Android development may look hard at first. But do you know something that can make it easier? There are many programming languages for android development out there, but Kotlin is the one that makes the process easier.
Kotlin is a programming language similar to Java and was initially developed for JVM(Java Virtual Machine). Kotlin is a more concise version of Java, making some statements easier to write. It provides many more features than Java, which makes it preferable. In this blog, we will be discussing the inline function in Kotlin.
Inline Function in Kotlin
An inline function in Kotlin is a higher-order function or lambda expression that is anonymous. You can use these expressions as usual functions or parameters for other functions. But sometimes, these high-order functions can increase the runtime overhead. To counter this, we use an inline function in Kotlin to improve the performance of the higher-order functions.
Inline functions tell the compiler not to allocate memory location to lambda expressions. Thus, by using the Inline function in Kotlin, you can copy the function parameters of Lambda expressions at the calling site.
Code
fun main(args: Array<String>) {
inlineFunction({
println("applying inline function")
})
}
inline fun inlineFunction(myFun: () -> Unit ) {
myFun()
print("code under inline function")
}
Output
Non-Local Control Flow
Kotlin does not allow the compiler to return from the lambda expression itself. In such cases, if we inline the lambda expression, the function where the inline was executed will return along with the lambda. These returns are located in the lambda and are called non-local returns.
For example:
Code
fun main(args: Array<String>) {
inlineFunction({
println("applying inline function")return
},
{
println("next parameter")
})
}
inline fun inlineFunction(myFun: () -> Unit, nxtFun: () -> Unit) {
myFun()
nxtFun()
print("code under inline function")
}
Output
Crossinline Keyword
In the non-local return, the lambda expression is returned along with the function where it was executed. To prevent this, we can mark the lambda using crossinline. This way it will throw a compiler error if there is any return request in the lambda expression.
For example:
Code
// Crossinline function in Kotlin
fun main(args: Array<String>) {
inlineFunction({ println("applying inline function")
return
// compile time error
},{ println("next parameter")})
}
inline fun inlineFunction(crossline myFun: () -> Unit, nxtFun: () -> Unit) {
myFun()
nxtFun()
print("code under inline function")
}
Output
Noinline
Suppose multiple lambdas are passing through the inline identifier and you want only some of them to be inlined. In that case, you can mark the function parameters which you don't want to be inlined with noinline modifiers.
For example:
Code
// Noinline function in Kotlin
fun main(args: Array<String>) {
inlineFunctionExample({ println("applying inline function")},
{ println("next parameter")
})
println("the main closing")
}
inline fun inlineFunctionExample(myFun: () -> Unit, noinline nxtFun: () -> Unit ) {
myFun()
nxtFun()
println("code under inline function")
}
Output
Reified Type Parameters
We use this to identify the type of parameter that passes during the runtime. For example:
Code
// Declaring Reified Type Parameters in Inline function in Kotlin
inline fun <reified A> myExample(name: A) {
println("Name of your website -> "+name)
println("Type of myClass: ${A::class.java}")
}
fun main() {
// Calling func() with String
myExample<String>("www.codingninjas.com")
// Calling func() with Int value
myExample<Int>(500)
// Calling func() with Long value
myExample<Long>(1L)
}
Output
Frequently Asked Questions
What are lambda expressions in kotlin?
These are anonymous functions that contain no name. You can use these expressions as usual functions or parameters for other functions.
What is a higher-order function in Kotlin?
A higher-order function is a parameter that is able to take a function as an argument and can also return a function passed as lambdas.
Is Kotlin good for App Development?
Kotlin is among the top choices for app development in today’s times. It is an alternative to java, which is used by app developers extensively.
In which fields is Kotlin used?
Developers can use Kotlin for App Development and web development for various platforms like macOS, IOS, and Android.
What is Kotlin?
Kotlin is a programming language used by android developers. It is used by most developers and is an open-source language developed by JetBrains.
Conclusion
This blog discussed and explained how to use the inline function in Kotlin along with some examples. We hope that we were able to make you understand this whole concept clearly. For more information on Kotlin look at some of these related articles below: