One of the most powerful features of Kotlin as a language that I have come to appreciate are extension functions.
Having worked in multiple languages in the JVM ecosystem before i
think first time experience with extension functions/properties have been one of the light-bulb moments that have glued me to Kotlin ever since.
What are extension functions you may ask?
Extension functions/properties is a Kotlin language feature that allows you to add new functionality to a class without necessarily having to implement it; ideally extending the class with new functionality.
Let's give an example of the java.util.Date class.
Among the many functionalities, the class lacks a way of formatting date to simple presentable strings. Most of the time you would create a utility function to handle these kinds of actions.
Extension functions make it possible to add custom functionality to already existing APIS, then using this new functionality as if it is part of the original API.
In our example above; probably you would have a utility class like the following.
object Util {
fun format(date: Date, pattern: String): String {
val dateFormat = SimpleDateFormat(pattern, Locale.getDefault())
return dateFormat.format(date)
}
}
Then we could use it as bellow
val formated=Util.format(date,"DD/MM/YYYY hh:mm a")
You notice our code is quite long. To solve these we are going to create an extension function. To declare an extension function, you prefix the name of the class you wish to extend (receiver type) to a function name. For example, in our date scenario, it will be Date.format. Notice date is our receiver type and format is our new extended function.
We, therefore, end up with the following simplified piece of code
fun Date.format(pattern: String):String{
val dateFormat = SimpleDateFormat(pattern, Locale.getDefault())
return dateFormat.format(this)
}
We can then use the format in any instance of date in our application as if it were part of the original definition of java.util.Date like below
val date = Date()
date.format("DD/MM/YYYY hh:mm a")
One thing to note; extension functions do not actually modify the classes they extend. You are not actually inserting new functions/properties to the class being extended but merely making some new interesting properties that were otherwise unavailable, available, and callable by the dot notation (.)
Perhaps you noticed the use of this in the extension snippet above.this just refers to the receiver object. In our case the java.util.Date. What it does is allow us to access interesting properties of the receiver object that we may need to construct our own extension.
Scope of extension functions
Perhaps you are wondering, now where do you place my extension functions in my application structure. Most of the time you would define extension functions as a top-level directly under packages. To use such an extension outside that package you simply import the function as if it were a class definition.i.e import com.joe.composer.extensions.format
in this case format was declared in a package named com.joe.composer in a file named extensions.
Also note an extension declared at the top level of a file has access to the other private top-level declarations in the same file and if an extension is declared outside its receiver type, it cannot access the receiver's private or protected members.
Benefits of extension functions
- They greatly reduce code.
- Ability to extend the capabilities of inbuilt classes and third party libraries is very powerful
- They make code easy to read by eliminating the need for utility functions and putting them right where they are needed hence easy to follow. Jetpack Compose has heavily utilized extension functions so it might be interesting to see the implementation of some of the composibles Hope you enjoyed this article on Extension Functions. Happy Coding!!!
Top comments (0)