Kotlin Inline Functions With Example
Kotlin Inline Functions request compiler to copy parameters and functions to the calling place and not allocate memory. So basically inline functions are used to reduce memory.
Kotlin Inline Functions are declared by using keyword inline. In inline functions, we can pass lambda functions to reduce memory.
For Example:
fun main(args: Array<string>) {
playerName({ println(“Cristiano”)})
}
playerName({ println(“Cristiano”)})
}
inline fun playerName(myCall: () -> Unit ) {
myCall()
print(“Ronaldo”)
}
noinline
When we want some lambda function to not be inclined in inline functions, then we use noinline modifier to mark that function.
For Example:
fun main(args: Array<string>) {
playerName({println(“Cristiano”)}, {println(“Lional”)})
}
playerName({println(“Cristiano”)}, {println(“Lional”)})
}
inline fun playerName(myCall1: () -> Unit, noinline myCall2: () -> Unit) {
myCall1()
myCall2()
println(“Neymar”)
}
Output:
Cristiano
Lional
Neymar
Non-local returns
A normal return statement is not allowed in lambda functions. If we want explicitly return from lambda, We have to use label. Because a lambda can not make enclosing function return but if the lambda function is passed in inline function then return is allowed.
For Example:
fun main(args: Array<string>) {
playerName({println(“Cristiano”) return}, {println(“Lional”)})
}
playerName({println(“Cristiano”) return}, {println(“Lional”)})
}
inline fun playerName(myCall1: () -> Unit, noinline myCall2: () -> Unit) {
myCall1()
myCall2()
println(“Neymar”)
}
Output:
Cristiano
crossinline
crossinline modifier is used to prevent return in lambda expression. If return is found in lambda expression then crossinline modifier will throw compile time error.
For Example:
fun main(args: Array<string>) {
playerName({println(“Cristiano”) return //compile time error}, {println(“Lional”)})
}
playerName({println(“Cristiano”) return //compile time error}, {println(“Lional”)})
}
inline fun playerName(crossline myCall1: () -> Unit, noinline myCall2: () -> Unit) {
myCall1()
myCall2()
println(“Neymar”)
}
Reified Type Parameters
If we want to access type of parameter passed during the call then we need to use reified modifier.
For Example:
fun main(args: Array<string>) {
playerName<string>()
}
playerName<string>()
}
inline fun
print(T::class)
}
Output:
class kotlin.String
Inline Properties
We can also inline properties and inline just specific accessor of property in kotlin programming language using inline modifier.
For inline properties
fun main(args: Array<string>) {
print(flag)
}
fun jerseyNo(i: Int): Int{
var j = i
return j
}
inline var flag : Boolean
get() = jerseyNo(7) == 7
set(flag) {flag}
print(flag)
}
fun jerseyNo(i: Int): Int{
var j = i
return j
}
inline var flag : Boolean
get() = jerseyNo(7) == 7
set(flag) {flag}
For inline accessor of property
fun main(args: Array<string>) {
print(flag)
}
fun jerseyNo(i: Int): Int{
var j = i
return j
}
var flag : Boolean
inline get() = jerseyNo(7) == 7
set(flag) {flag}
print(flag)
}
fun jerseyNo(i: Int): Int{
var j = i
return j
}
var flag : Boolean
inline get() = jerseyNo(7) == 7
set(flag) {flag}