July 2018
Intermediate to advanced
400 pages
12h 14m
English
Your first extension allows you to add a specified amount of enthusiasm to any String. Define it in Extensions.kt:
Listing 18.1 Adding an extension to String (Extensions.kt)
fun String.addEnthusiasm(amount: Int = 1) = this + "!".repeat(amount)
Extension functions are defined in the same way as other functions, with one major difference: When you specify an extension function, you also specify the type the extension adds functionality to, known as the receiver type. (Recall from Chapter 9 that the subject of an extension is called a “receiver.”) For the addEnthusiasm function, the receiver type you specified is String.
addEnthusiasm’s function body is a single-expression function that returns ...
Read now
Unlock full access