Naming parameters of function types

So far, we've defined all our function type parameters without names. Kotlin allows function type parameters to have names. Let's go back to our long-running task example and show how we can name the parameters:

fun longRunningTask(onFinished: (result: Any) -> Unit, onError: (fail: Throwable) -> Unit) 

The benefit of this is code completion inside the IDE. But, this doesn't force you to name your lambda arguments like this. You can then call the function with the parameter names from the function type:

longRunningTask({ result -> println("Result: $result") }, { fail -> println("Error: ${fail.message}") })

Or, you can use your own names, as you can see here:

longRunningTask({ r -> println("Result: $r") }, { err -> println("Error: ${err.message}") })
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.195.146