July 2018
Intermediate to advanced
400 pages
12h 14m
English
What if you wanted to print the string "Madrigal has left the building" both before and after calling addEnthusiasm on it?
First, you would need to make the easyPrint function chainable. You have seen chained function calls before; functions can be chained if they return their receiver or another object that subsequent functions can be called on.
Update easyPrint to make it chainable:
Listing 18.5 Making easyPrint chainable (Extensions.kt)
fun String.addEnthusiasm(amount: Int = 1) = this + "!".repeat(amount) fun Any.easyPrint()= println(this): Any { println(this) return this } ...
Now, try calling the easyPrint function two times: once before addEnthusiasm and once afterward:
Listing 18.6 Calling ...
Read now
Unlock full access