When using properties, we often encounter certain patterns over and over again. Implementing these manually for every property would prove tedious, and we'd prefer to implement them once and then reuse them. These kinds of patterns include the following:
- Lazy access—a property value should be computed the first time it is accessed and reused on subsequent accesses.
- Notify listeners of a change to one of the values of properties. Have you ever coded in C#? If you have, then I am sure the INotifyPropertyChange interface will come to mind.
- Use a map to store your fields rather than a materialized field.
Well, good news! Kotlin's delegate properties support all of these. We deal quite often with types for which we need an identifier, as shown in the following code:
interface WithId { val id: String } data class WithIdImpl(override val...