Lesson 41 – Extension Functions In Kotlin language

Lesson 41 – Extension Functions In Kotlin language

Hello viewer, You can now have our Tutorial Lessons in your android mobile device and read it offline.
Download kotlin Programming APP on PlayStore
Download Website SEO Lessons APP on PlayStore

Another very useful feature of Kotlin for programmers is the concept of extension functions.

Using the extension functions Kotlin allows us to add other methods to an existing class without having to inherit from it.

With a series of examples, this feature will be clear.

Problem 1

Add two functions to the String class that allows you to retrieve the first half and the second half.

Project 158

fun String.firsthalf (): String {
    return this.substring (0..this.length / 2-1)
}
// www.coding180.com
fun String.secondhalf (): String {
    return this.substring (this.length / 2..this.length-1)
}

fun main (args: Array <String>) {
    val string1 = "Wind"
    println (string1.firsthalf ())
    println (string1.secondhalf ())
}

To define an extension function to a class, we must only precede the name of the function to which class we are extending:

Fun String.firsthalf(): String {
    Return this.substring (0..this.length / 2-1)
}

Within the function using the keyword this we can access the object that called that function (in our example as we called in firsthalf through the object string1 we have access to the string “Wind”)

When we call an extension function, we do it identically to the other functions that the class has:

    Println (string1.firsthalf())

The second function returns a String with the second half of the String:

Fun String.secondhalf(): String {
    Return this.substring (this.length / 2..this.length-1)
}

We can define extension functions that overwrite existing functions in the class, that is, we could create a function named capitalize() in the String class and define a new functionality.

Problem 2

Add a function to the IntArray class that allows you to print all its components on one line.

Project159

fun IntArray.print () {
    print ("[")
    for (element in this) {
        print ("$element")
    }
    println ("]");
}
// www.coding180.com
fun main (args: Array <String>) {
    val array1 = IntArray (10, {it})
    array1.print ()
}

We define an extension function called print in the class IntArray, in its algorithm by a for we print all its components:

fun IntArray.print () {
    Print ("[")
    For (element in this) {
        Print ("$element")
    }
    Println ("]");
}

In the main, we define an array of 10 elements IntArray and store values 0 through 9 using a lambda expression.

Since the IntArray class now has a function that prints all its components we can directly call the print method of the IntArray class:

Fun main (args: Array <String>) {
    Val array1 = IntArray (10, {it})
    array1.print ()
}

Problems proposed

  • Add to the String class a print method that shows all the characters that you have stored on a line.
  • Codify the function of extension called “until” that must extend the class Int and aims to show from the integer value that stores the object until the value that arrives as parameter:
    Fun Int.until (fin: Int) {
    
Solution

About The Author

Related posts

Leave a Reply