In typical programming, many functions contain only one expression. Here is an example of this kind of function:
fun square(x: Int): Int { return x * x }
Another one, which can be often found in Android projects, is a pattern used in Activity, to define methods that just get text from some view or provide some other data from the view to allow a presenter to get it:
fun getEmail(): String { return emailView.text.toString() }
Both functions are defined to return the results of a single expression. In the first example, it is the result of an x * x multiplication, and in the second one it is the result of the expression emailView.text.toString(). These kinds of function are used all over Android projects. Here are some common use cases:
- Extracting some small operations (like in the preceding square function...