Kotlin Property delegation

This time I bring you a pretty interesting article for making your properties much more powerful!

With properties delegation, an object will be in charge of returning and setting the value of your properties, which will let you do really cool things. Check the article today for some examples:

As we’ve seen in previous articles, properties need a default value, they can’t be declared without assigning them a value.

This is a problem, because imagine that you want to store a view into a property. As this code is executed during object creation, you can’t access the context at that time.

What can you do?

Kotlin Property delegation: delegate the value of a property to another object

The property delegation will use another object that is able to return a result when calling get and set (if var is used).

In a world where we have no control over the creation of many objects, such as the Android framework, this delegation will save our lives in many cases.

I’ll show you three examples that I find very useful in Android

Setting a view to a property

For this we have two options using delegation, and prevent using null, (something not recommended if you can avoid it).

The first one I like the least, because it forces you to use var for a property that could be immutable, and is also less secure.

With the reserved word lateinit we say to that property that it’ll not be empty, but that we still don’t have its final value:

In the onCreate we can assign the final value:

This isn’t really a delegate, although it does the same operation as the delegate notNull, which was relegated in favor of this first.

The second option is much more elegant. It consists of using the lazy delegate, which won’t execute the code that is indicated until the property is called for the first time:

The findView won’t run until the get of the textView is called for the first time. It’s safer because you can’t change the value by mistake, and it doesn’t force us to remember to set it after the setContentView.

At the moment we do:

The code will be executed in lazy form.

As you can see, the way to delegate is by using the reserved word by.

Let’s see another example

Notify changes to an adapter

In an adapter we can have an items property that automatically launches an adapter update every time it is set:

It simply sets an initial value, and then calls the function that is defined after each modification.

In this case I’m just calling notifyDataSetChanged, but as you see, the function receives the old and new values, so technically you could check what the changes are and update only the difference.

If you’re interested in this example, I wrote about it extensively in another article.

Declare the Dagger graph in a lazy way

It’s another of the situations in which I have found this functionality very useful.

Returning to lazy, you can use it to declare the component of the application during the declaration of the property:

This way you don’t need to use lateinit, and the property becomes immutable.

You can do the same if you are using subcomponents in the activities:

New in Kotlin 1.1: Local delegated properties

We’ve already seen how useful delegation is to give extra abilities to the properties in our classes. But lazy for instance, would also be really helpful too on variables, and Kotlin was lacking this feature.

Now, with local delegated properties, we can do it:

Though this example could be resolved without using lazy delegation, it helps understand the concept.

We have a couple of heavy objects that may or may not be used. By using lazy, we can delay the instantiation until we are sure we are going to use them.

The first time is used, the code inside the braces is executed, and it will be cached in case it’s used again later.

Conclusion

The property delegation will help you make properties much more powerful and simplify and reuse code.

I’d love to see if / where you’re using Kotlin, so please drop me a comment or leave a response if you have anything to share (or even any questions to ask!)

Happy Coding!

Facebook Comments
Get free email updates!
Signup now and receive an email once we publish new content.
We respect your privacy

Juma Allan

In this blog, I write tutorials and articles related to Android and Node Js coding. I am an Open Source Enthusiast and have been programming Android for three years now. I love sharing my knowledge with others, and learning new languages everyday. I write on this blog to share what I've gained by learning from the web and friends.