February 2018
Intermediate to advanced
350 pages
7h 35m
English
Extension functions can be declared as members of a class. An instance of a class with extension functions declared is called the dispatch receiver.
The Caregiver open class internally defines, extension functions for two different classes, Feline and Primate:
open class Caregiver(val name: String) { open fun Feline.react() = "PURRR!!!" fun Primate.react() = "*$name plays with ${this@Caregiver.name}*" fun takeCare(feline: Feline) { println("Feline reacts: ${feline.react()}") } fun takeCare(primate: Primate){ println("Primate reacts: ${primate.react()}") }}
Both extension functions are meant to be used inside an instance of Caregiver. Indeed, it is a good practice to mark member extension functions as private, ...
Read now
Unlock full access