Property delegation (standard delegates)
In the previous section, where we discussed delegation, we learned that delegation is a technique of method passing/forwarding.
For property delegates, it almost does the same. A property can pass its getter and setter calls to the delegate and the delegate can handle those calls on behalf of the property itself.
You're probably thinking, what is the benefit of passing getter and setter calls to the delegate? Only the delegate you're using can answer this question. Kotlin has multiple predefined standard delegations for most common use cases. Let's have a look at the following list, containing available standard delegates:
- The
Delegates.notNull
function andlateinit
- The
lazy
function - The
Delegates.Observable
function - The
Delegates.vetoble
function
The Delegates.notNull function and lateinit
Think of a situation where you need to declare a property at the class level, but you don't have the initial value for the variable there. You'll get the value at some...