Custom delegates

All previous delegates came from the standard library, but we can easily implement our own property delegates. We've seen that in order to allow a class to be a delegate, we need to provide the getValue and setValue functions. They must have a concrete signature, but there is no need to extend a class or implement the interface. To use an object as a delegate, we don't even need to change its internal implementation, because we can define getValue and setValue as extension functions. However, when we are creating custom classes to be delegates, then an interface may be useful:

  • It defines a function's structure, so we can generate proper methods in Android Studio.
  • If we are creating libraries, then we might want to make delegate ...

Get Android Development with Kotlin now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.