In this article we will look at lazy binding or delegation in Kotlin. The main focus of the article is show you very high level picture of what delegation and then show you awesome factory methods shipping Kotlin standard library to make your life happy.

What’s Delegation?

In general, Delegation refers to passing responsibility of one to another in order to achieve code-reuse in Object Oriented Programming.
It prevents implementing the same code again and again.

Delegation in Kotlin

Kotlin supports delegation pattern natively without any boiler plate code.

There are 2 types of Delegation in Kotlin:

  1. Class Delegation

Kotlin provides Standard Delegation methods for various cases of delegation in the standard library.

We will cover Standard Delegation and make you aware about awesome features shipping in the standard library. I find them really cool ;)

Standard Delegates

In the previous articles, we saw how to declare properties in Kotlin either by using var and val. These properties can’t be declared without a default value to them due to which we set it to null

val toolbar: Toolbar // No default value, error!
val toolbar: Toolbar? = null // Fixed. But it's null now

To immediately fix it we set the default value to null but that’s really bad cause now we need to always do null check before accessing the object.

if (toolbar != null) {
// Do your job like setTitle, etc
}

We wish for a way to write the code like this:

val toolbar: Toolbar = findViewById(R.id.toolbar) as Toolbar

This way we can set the variable as val and prevent setting default value or setting it as null type. Unfortunately we can’t do that because the context is not ready yet.

You wish

Fortunately with Kotlin there is a better way to do this called Property Delegation

Lazy

Lazy is a function in standard library to delegate the implementation of the property. Confusing, right? let’s look at some code cause we understand code better than words.

val toolbar: Toolbar by lazy {
findViewById(R.id.toolbar) as Toolbar
}

The lazy block won’t execute until the toolbar variable is called for the first time. Now you can initialize at the property level before the context is ready.

// Context is prepared over here
setContentView(R.layout.kotlin_toolbar)
// Now the lazy block is executed and stored
toolbar.setTitle("You rock Kotlin!")

Under the hood
The first call to toolbar executes the lazy block and the subsequent calls just get the remembered value. Internally it creates a delegate object which stores the value and a getter to access it. The evaluation of lazy is synchronized, the value is computed only in one thread, and all threads will see the same value. It can only be used with val

lateinit

lateinit is a keyword unlike lazy which is a function. It indicates the property will not be empty and value is coming soon.

lateinit var toolbar: Toolbar

To use

// Context is prepared over here
setContentView(R.layout.kotlin_toolbar)
toolbar = findViewById(R.id.toolbar) as Toolbar
toolbar.setTitle("You rock Kotlin!")

Similar to lazy the value of toolbar is set in lazy fashion after the context is ready.

Under the hood
The value of lateinit variable can be initialized from anywhere the object is visible from. Internally it generates a backing field which stores the value where in lazy a delegate object stores the value and getter to access it. The lateinit keyword can only be used with var .

Observable

Another way for delegation in Kotlin. Coming soon, probably in next articles.

Conclusion

Delegation is extremely powerful depending upon the use case. It helps us write code which we wished for. Kotlin continues to impress us and changes the way we write code to build beautiful and better apps.

We will continue with this series and with each article will dive deep into amazing features of Kotlin and most importantly understand how they work.

Google Developer Expert @ Android | Android Engineer @ Clue | Instructor @Caster.IO

The (retired) Pub(lication) for Android & Tech, focused on Development