Kotlin, Zero to Kotlin Hero

Zero to Kotlin Hero: Kotlin Extensions

Yeah, Let’s talk about Kotlin Extensions.

The previous article covers data classes and sealed classes in Kotlin. This article covers Kotlin Extension Functions and Extension Properties.

In a previous article, I wrote about Kotlin Classes and Object Oriented Programming. Based on this, if all you want to extend a class with new functionality, you might be thinking about Inheritance or using a design pattern to do this.

Kotlin introduces something called Extension Functions and Extension properties to help you achieve this without using any of the methods stated above.

Okay… What are Extensions?

Kotlin Extensions add some functionality in existing classes without extending the class. Kotlin supports both Extension Functions and Extension Properties.

Kotlin Extension Functions.

When writing an extension function, it should be prefixed with the className that is to be extended. In an example below, I would show how to declare an extension function for a String class that checks if the String is a palindrome.

We would call the function palindrome() and would prefix the name of this function with the className String. These class names are also called receiver types.

fun Boolean String.palindrome(){

  // palindrome logic

}

Kotlin Extension Properties.

Similar to extension functions, extension properties are prefixed with the className.

val <T> List<T>.lastIndex: Int
get() = size - 1

Note that, since extensions do not actually insert members into classes, there’s no efficient way for an extension property to have a backing field. This is why initializers are not allowed for extension properties. Their behavior can only be defined by explicitly providing getters/setters. – Kotlin Documentation

What this means is that when defining extension properties we have to add our logic to the getters/setters. Looking at the code snippet defined above, the line val List.lastIndex: Int shows that we are adding an extension property lastIndex to Kotlin Lists.

Now, after defining the property we have to define its behavior (what should happen when it is called). Since it is not an extension function, we have to define it by using get() or set() depending on what we are trying to achieve. The lastIndex function uses a getter.

 

How do we use Kotlin Extensions?

Now that we have seen how to declare extension properties and extension functions, let’s see how to use them:

The palindrome() extension function defined on the String type can be called like so:

val myString = "madam"

val isPalindome = myString.palindrome()

The lastIndex extension property defined on the List type can be called like so:

var myList: List<Int> = listOf(1,2,3,4)

val last = myList.lastIndex

 

How does this work?

Extensions in Kotlin are resolved statically. This means they are like regular static functions. They take in an instance of the receiver class as a parameter when the function is defined.

 

Conclusion

We have seen Kotlin extension functions and extension properties. Now, use them in your projects when you need to. I normally create a Utils.kt file and write all my Extension functions in that file. You can do it that same way or whichever works for you.

Get ready to know about the Anko library in the next article!

 

Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *