Updated April 10, 2023
Introduction to Kotlin Inline Function
The kotlin inline function is one of the types of function by using the keyword inline. It is used for to enhance the performance of the higher-order function also these inline function which helps to call and tell the compiler for to copy the parameters and functions. it does not support the local and virtual types of functions also like nested classes, function expressions, and other default values of parameters. It may be the optional data types like integer, array, or string parameters that will pass the anonymous function or lambdas on the script.
Syntax
In kotlin language have many default keywords, variables, and functions for utilizing and create an application with different features. Among these inline functions is one of the performance and higher-order functions for which the compiler tells and copied the parameter type and functions to the specific code site.
fun main(args:Array<String>)
{
functionName()
{
---some kotlin script logic codes depends upon the requirement---
}
inline fun functionName()
{
---some logics based on the kotlin inline function with lambda or any other expressions---
}
}
The above codes are the basic syntax for creating and utilizing the inline function in the kotlin application.
How inline function works in Kotlin?
The inline function is one of the default functions in the kotlin language that can be basically used to requesting the compiler to copy the inlined code at the calling place on the script. If the program is compiled using the compiler and it keeps the wrapper classes for each inline class and the instances can be represented at the runtime for each the wrappers are each set of inline classes and their instances can be represented at runtime with the underlying class type. This what we used earlier in the other languages like java Int can be represented as the primitive int or as the Integer. The kotlin compiler will prefer using the underlying types instead of wrappers to produce the most performance and optimized the code however sometimes it is necessary to keep wrappers around the classes as a rule of thumb with the inline classes are boxed and whenever they are used with another type. So that the inline classes are may be represented as both underlying value and with the wrapper, referential equality is pointed about for them and it is, therefore, prohibited on the kotlin codes. Since the inline is compiled to the underlying type it may be lead to various obscure errors and for to perform with the unexpected platform about the signature clashes on the codes.
Examples of Kotlin Inline Function
Let us discuss examples of Kotlin Inline Function.
Example #1
Code:
package one;
fun demo(exam: (id: String)->Unit, id: String){
println("Welcome To My Domain its the first example regarding kotlin inline function")
println("Have a Nice Day users")
exam(id)
}
inline fun demo1(exam: (id: String)->Unit, id: String){
println("Welcome To My Domain its the first example regarding kotlin inline function")
println("Have a Nice Day users please try again")
exam(id)
}
class Second {
var stdid:Int = 0
var stdName:String="sivaraman"
var stdRollno:Int=13
var stdAddress:String="Flat 12C, Aravind Enclave, Sathyamoorthy street, Srinivasa nagar, chengalpattu district, chennai-600009"
fun stddets(){
println("Welcome To My Domain this is the second example reagrding the kotlin companion object")
println("${this.stdid}")
println("${this.stdName}")
println("${this.stdRollno}")
println("${this.stdAddress}")
println("We have entered the student details like student id, student name, student roll number, student Address etc the information’s are encrypted and it is stored in the separate database in cloud applications")
}
companion object eg{
var p: Int = 3
var empid:Int = 0
var empName:String="sivaraman"
var empSalary:Int=13000
var empAddress:String="Flat 12C, Sathya Enclave, Sathyamoorthy Nagar, Muthamizh Theru, chengalpattu district, chennai-600010"
fun details(){
println("Your details are: $p")
p++
}
fun empdetails(){
println("Welcome To My Domain this is the second example regarding the kotlin companion object")
println("${this.empid}")
println("${this.empName}")
println("${this.empSalary}")
println("${this.empAddress}")
println("We have entered the student details like employee id, employee name, employee salary, employee Address etc the information’s are encrypted and it is stored in the separate database in cloud applications")
}
}
}
fun main() {
demo({ id: String ->
println("Thank you users for spending the time")
println("Kindly keep trying and spent your valuable time with us $id")
}, "Welcome Users")
Second.details()
Second.eg.details()
Second.eg.empdetails()
println("Thank you users for spending the time with the companion object application")
}
Output:
In the above example, we used companion object with classes to perform the operations in the inline functions.
Example #2
Code:
package one;
fun demo(exam: (EmpId: String)->Unit, EmpId: String){
println("Welcome To My Domain its the second example that relates to the kotlin inline function")
println("Have a Nice day users please try again")
exam(EmpId)
}
inline fun demo1(x: Int, eg: () -> Unit):Int {
eg()
return 7*x
}
fun main() {
demo({ EmpId: String ->
println("Please keep on spending the time with our application")
println("Kindly keep trying and spent your valuable time with us $EmpId")
}, "Hello Users")
var x = 7
println(demo1(x, {println("Welcome Your: Kotlin Inline Functions are")}))
}
Output:
In the second example, we used lambda expressions to perform the inline function calculations.
Example #3
Code:
package one;
inline fun <reified T> demo(ls: List<Any>): Boolean {
ls.forEach {
if (it is T) {
System.out.println("Welcome To My Domain its the third example that related to the kotlin inline function")
return true
}
}
System.out.println("Thank you users kindly write the code that related to the kotlin inline function")
return false
}
fun main(args: Array<String>) {
demo<String>(listOf("raman", 2, args))
val second = mapOf<String, Int>("Employee ID" to 2001, "SNO" to 7536, "ProofID" to 12134)
for ((k,v) in second) {
println("Your input key is: $k and your input value is $v")
}
second.forEach { (k, v) ->
println("Your input key is: $k and your input value is $v")
}
println("Your input map size is: ${second.size}")
println("Your input entries are: ${second.entries}")
}
Output:
In the final example, we used a map interface additionally to perform the operations in the inline functions.
Conclusion
The kotlin language has many default functions to perform the operations in the back end. In mobile applications, these operations and features are varied from one to the others. Like that kotlin, inline functions are to perform the higher-end application and it optimizes the codes to run the application.
Recommended Articles
This is a guide to Kotlin Inline Function. Here we discuss the introduction, syntax, and working of inline functions in kotlin along with different examples and its code implementation. You may also have a look at the following articles to learn more –