Kotlin provides the ability to extend a class or an interface 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 or an interface 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.
To declare an extension function, prefix its name with a receiver type, which refers to the type being extended. The following adds a swap function to MutableList<Int>:
fun MutableList<Int>.swap(index1: Int, index2: Int) {
val tmp = this[index1] // 'this' corresponds to the list
this[index1] = this[index2]
this[index2] = tmp
}
The this keyword inside an extension function corresponds to the receiver object (the one that is passed before the dot). Now, you can call such a function on any MutableList<Int>:
val list = mutableListOf(1, 2, 3)
list.swap(0, 2) // 'this' inside 'swap()' will hold the value of 'list'
Note: You can declare extensions for one class inside another class. Not necessarily at the top level only
Using higher-order functions imposes certain runtime penalties
In Kotlin, the higher-order functions or lambda expressions, all stored as an object so memory allocation, for both function objects and classes, and virtual calls might introduce runtime overhead. Sometimes we can eliminate the memory overhead by inlining the lambda expression. In order to reduce the memory overhead of such higher-order functions or lambda expressions, we can use the inline keyword which ultimately requests the compiler to not allocate memory and simply copy the inlined code of that function at the calling place.
fun higherfunc( str : String, mycall :(String)-> Unit) {
// inovkes the print() by passing the string str
mycall(str)
}
fun main(args: Array<String>) {
print("GeeksforGeeks: ")
higherfunc("A Computer Science portal for Geeks",::print)
}
It create to much overhead if we check the bytecode by Tools -> Kotlin -> Show Kotlin Bytecode.
inline fun higherfunc( str : String, mycall :(String)-> Unit) {
// inovkes the print() by passing the string str
mycall(str)
}
fun main(args: Array<String>) {
print("GeeksforGeeks: ")
higherfunc("A Computer Science portal for Geeks",::print)
}
With the help of the inline keyword, the println lambda expression is copied in the main function in the form of System.out.println and no further calls required.
Extension Function is a function added in the class which is not open.
Extension function declared outside of the class so it can not access the class members
Inline function reduce the overhead of lamda function calling. when you pass a function as a argument then kotlin indirectly create an instance and then call a function which create an overhead. if we make that function inline then a function code is pasted at a place of call and no instance created. you can check by java byte code tool.
Extension function
Inline function. in main function the code of calfulcatetimeandrun()'s code is pasted.