In Kotlin, higher-order functions are really important, and so it is also important to make their usage as easy as possible. This is why Kotlin introduced a special convention that makes higher-order functions simpler and clearer. It works this way: if the last parameter is a function, then we can define a lambda expression outside of the brackets. Let's see how it looks if we use it with the longOperationAsync
function, which is defined as follows:
fun longOperationAsync(a: Int, callback: ()->Unit) { // ... }
The function type is in the last position in the arguments. This is why we can execute it this way:
longOperationAsync(10) { hideProgress() }
Thanks to the last lambda in an argument convention, we can locate the lambda after the brackets. It looks as if it is outside the arguments.
As an example, let's see how the invocation of code in another thread can be done in Kotlin. The standard way of starting a new...