October 2019
Intermediate to advanced
434 pages
11h 54m
English
Single expression functions are means of reducing boilerplate when writing functions in Kotlin, by allowing us to remove the curly braces and define a function as a single expression. To demonstrate the usage of single expression functions, we'll start with the following simple logMessage function:
fun logMessage(msg: String) { println(msg)}
This function body is a single simple line. Kotlin allows us to rewrite this function using a single expression function like the following:
fun logMessage(msg: String) = println("")
To make a function a single expression function, we can add the = operator after the closing parentheses and then define the expression to use as the function implementation. The same is possible ...
Read now
Unlock full access