July 2018
Intermediate to advanced
400 pages
12h 14m
English
Kotlin allows you to reduce the amount of code required to define a function like castFireball or formatHealthStatus that has only one expression – that is, one statement to be evaluated. For single-expression functions, you can omit the return type, curly braces, and return statement. Make those changes to your castFireball and formatHealthStatus functions, as shown below:
Listing 4.6 Using optional single-expression function syntax (Game.kt)
...private fun formatHealthStatus(healthPoints: Int, isBlessed: Boolean): String {val healthStatus = when (healthPoints) {private fun formatHealthStatus(healthPoints: Int, isBlessed: Boolean) = when (healthPoints) { 100 -> "is in excellent condition!" ...
Read now
Unlock full access