Using Kotlin idioms
Kotlin provides a set of idioms that allows us to drastically reduce the amount of boilerplate code. Boilerplate refers to sections of code that have to be included in many places with little or no alteration. In this section, we will learn some of the most used idioms.
Inferred types
We may have a function written that returns a value, such as:
fun lower(name : String) : String { val lower : String = name.toLowerCase() return "$name in lower case is: $lower" }
Here, we are explicitly indicating the type of the result of the function and the internal variable that we use inside.
In Kotlin, we could infer the type of the variable:
fun lower(name : String): String { val lower = name.toLowerCase() return "$name in lower case is: $lower" }
And even the return type of our function could be inferred:
fun lower(name : String) = "$name in lower case is: ${name.toLowerCase()}"
This will be extremely useful as the code that we created with the inferred type did not need to change...