What is the Purpose of the Kotlin Extension Function?
The Kotlin extension function is defined as a function derived from a class that is not implemented within that class rather it is implemented outside that class. In this way, you can easily extend the functionality of a class by creating a function outside the scope of this class with the desired functionality by making use of the class name. To understand the working of the Kotlin extension function, you will have to go through the sections of this article discussed below.
Using the Kotlin Extension Function
To learn how the Kotlin extension function is used in Ubuntu 20.04, you can take help from the following three examples:
Example # 1: Using the Kotlin Extension Function with the String Class
In this example, we will be using the extension function with the built-in String class of the Kotlin programming language. We want to create a function that is capable of printing a string while truncating its first and last element. This function is not implemented in the String class by default. Therefore, we will attempt to implement it by making use of the extension function while using the Kotlin script shown below:
In this example, we have first defined the name of our function starting with the name of the class i.e., String. We have named our function as “removeInitialAndEndChar”. The return type of this function is String. Then, we have defined the functionality of this function with the statement “this.substring(1, this.length-1)”. Here, the substring function used is a built-in String class function. Now, within our “main()” function, we have declared a string with the name “newString” that we want to truncate from the beginning and the end. Then, we have created an output variable to which we have assigned the result of our “removeInitialAndEndChar()” function that we have called with our declared string. Finally, we have used a “println” statement for printing the output of this function on the terminal.
The compilation of this Kotlin script will take place with the execution of the following command:
Then, the execution of this script will take place by running the command shown below:
Our string after removing its first and last character is shown in the following image:
Example # 2: Using the Kotlin Extension Function with the Mutable List Class
This is another example of using the extension function of the Kotlin programming language. We will now try to use this function with the Mutable List class of the Kotlin programming language. The Kotlin script shown below depicts this:
In this example script, we wanted to create a function that will be capable of swapping any two given elements of a list. We have extended this function from the Mutable List class of the Kotlin programming language. The defined function accepts two integers i.e., index1 and index2. Also, this function is capable of returning the integer list after swapping its elements. Within this extended function, we have created a value named “temp” and have assigned to it the first index passed to this function.
Then, we have assigned the second index to the first index. After that, we have assigned the value of the “temp” variable to the second index. Finally, we have returned this integer list. Within our “main()” function, we have created a mutable list have assigned to it six integer elements. After that, we have printed the elements of this list before swapping. Then, we wanted to swap the second element of this list with the sixth element because of which we have passed the indexes “1” and “5” to the swapping function and have assigned its result to the value “result”. Finally, we have printed this list after swapping its elements on the terminal.
The output of this Kotlin script is shown in the following image which shows the elements of our list both before and after swapping.
Example # 3: Using the Kotlin Extension Function with a User-Defined Class
In this example, we will be using the extension function with a user-defined class. For understanding that, you need to take a look at the Kotlin script shown in the image below:
In this script, we have first created a class named “Person”. In this class, we have a function named “lifeStatus” which accepts a string argument. Within this function, we just want to notify whether a person is alive or deceased. Now, we want another function that will be capable of printing the age of a person. As you can see, we have not implemented this function within the “Person” class; therefore, we will extend it from the “Person” class.
We have created an extended function named “age” that takes an integer argument “age”. Within this function, we have simply printed the age of the person. After that, we have generated an instance of the “Person” class named “P1”. Then, we have called both of our functions i.e., “lifeStatus()” and “age()” one by one by using this object.
The output of this Kotlin program is shown in the following image which implies that the extension function of Kotlin works perfectly fine with the user-defined classes as well.
In this article, we wanted to introduce you to the concept of the extension function in the Kotlin programming language in Ubuntu 20.04. After explaining the basic purpose of this function, we shared with you some examples in which we have implemented this function with built-in as well as with user-defined classes in Kotlin. This article will help you a lot whenever you will need to extend the functionality of any desired class in Kotlin.