October 2019
Intermediate to advanced
434 pages
11h 54m
English
Often, we work with classes and APIs we do not control. This can mean we are required to write code based on the style or requirements of those external parties. Extension functions provide a means of extending or modifying code we do not control. For example, we could write an extension function to determine whether or not String equals "Kotlin".
In the following snippet, we've written an isKotlin() function. This function is an extension function of the String type. This means that it's only available to instances of the String receiver type. Note that the function name is defined by first specifying the receiver type, in this case String, and then adding . and then the function name:
fun String.isKotlin() = this == ...
Read now
Unlock full access