August 2018
Beginner
178 pages
4h 56m
English
Now that you know how to define a function type, let's see how we can make use of them.
Calling a function type is the same as calling a normal function. You put parentheses after the function variable or the argument that is a function type. Here's a function that simulates a long-running task and accepts two callback functions, one in the case of successful completion and one in the case of an error. Inside the function, you can see how we call the onFinished and onError function type arguments:
fun longRunningTask(onFinished: (Any) -> Unit, onError: (Throwable) -> Unit) { try { //something long running onFinished("got result") } catch (fail: Throwable) { onError(fail) }}
All function types also have the invoke method ...
Read now
Unlock full access