The by Keyword
Delegate interface implementation.
What Is Delegation?
Class delegation lets one object hand off an interface's implementation to another object. Kotlin builds this into the language with the by keyword.
It is composition made effortless.
An Interface to Delegate
Start with an interface and a concrete implementation. We will later delegate to this implementation.
interface Speaker {
fun speak(): String
}
class LoudSpeaker : Speaker {
override fun speak() = "HELLO!"
}
fun main() {
println(LoudSpeaker().speak())
}