Press enter to see results or esc to cancel.

Lesson 41 – Extension Functions In Kotlin language

Share this

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)
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.


fun IntArray.print () {
    print ("[")
    for (element in this) {
        print ("$element")
    println ("]");
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) {
[sociallocker id="958"]
fun String.print () {
println (this)

fun main (args: Array <String>) {
"Hello World". print ()
val string1 = "End"
string1.print ()

Project 161
fun Int.until (fin: Int) {
    for (value in this..fin)
        print ("$value-")
    println ()

fun main (args: Array <String>) {
    val v = 10
    v.until (100) (10)


How useful was this Lesson?

Click on a star to rate it!

Average rating / 5. Vote count:

Be the first to rate this post.!

As you found this post useful...

Follow us on social media!

We are sorry that this lesson was not useful for you!

Let us improve this lesson!

Tell us how we can improve this lesson?

Share this