Extension functions are a really cool feature that Kotlin provides, and that you’ll find yourself using a lot when writing Android Apps.
We have to admit that the Android Framework sometimes makes things a bit difficult, and in Java the only solution we have left is to create wrappers that do things like we want, or utility clases with static methods that are not the best in terms of readability.
How would you like to be able to add extra functions to the Framework classes? This is what the Kotlin extension functions allow us to do.
Extension functions in Kotlin
Extension functions are functions that, as the name implies, help us to extend the functionality of classes without having to touch their code. Now let’s see how these functions are defined, and some examples that I personally find very useful.
How can you define an extension function?
Just write a function as you would normally, and put the name of the class before separated by a point. These functions can be everywhere, usually in an
Extensions
file which doesn’t even need to contain a class.
Very simple example: we want to make a view have the
visible()
, which makes it visible. We would write something like this:
I’ve written
this
so you can see that we can use the functions and properties of that class as if we were inside the class itself, but you can skip it:Some interesting examples
There are a couple of examples that I like to explain, because they perfectly show the power of extension functions.
The first is useful when you are inflating a view inside an adapter. Normally you would use something like this:
The line that inflates the view and uses
parent
is too complex, and 99% of the time is usually the same on any adapters. Why not give ViewGroups
the ability to inflate views?
Now you can use it in the code above:
A very similar example can be done with images. If you use Picasso library, for example, you need to load the image using the typical builder:
How would you like to be able to tell
ImageView
to upload a url by itself?Extension properties
Just as you can do extension functions, so can you do with properties. The only thing you need to remember is that extension properties can not save state, but will need to use existing functions to request or modify the state of the object:
This property retrieves the children of a
ViewGroup
.
Now you could iterate over them directly:
Note:it
is a reserved word that is used to access the input value of the function, when there’s only one. As we have seen in other articles, you can name those input values, and write the left par of the lambda when there are more than one.
Conclusion
With extension functions and extension properties, you can extend any class (even if you don’t have access to the code) and then use those functions and properties as if they were part of the class. The only thing you will see is an extra import in the file where it’s used.
origin: https://antonioleiva.com/
Comments
Post a Comment