October 2019
Intermediate to advanced
434 pages
11h 54m
English
Kotlin supports the notion of functions defined within other functions. This type of function is known as a local function. Local functions can be defined quite similarly to other functions, with two exceptions:
To better understand local functions, we'll use an example. In this code, we've defined two local functions, handleSuccess and handleError, within the handleNetworkResponse function:
fun handleNetworkResponse(response: Response<Person>) { fun handleSuccess(person: Person) { // handle successful response } fun handleError(error: Throwable?) { // handle error } when (response.isSuccess) { true -> handleSuccess ...
Read now
Unlock full access