July 2018
Intermediate to advanced
400 pages
12h 14m
English
In Kotlin, an anonymous function can modify and reference variables defined outside of its scope. This means that an anonymous function has a reference to the variables defined in the scope where it is itself created – as in the case of the configureGreetingFunction function you saw earlier.
As a demonstration of this property of anonymous functions, update the runSimulation function to call the function returned from configureGreetingFunction multiple times:
Listing 5.14 Calling println twice in runSimulation (SimVillage.kt)
...
fun runSimulation() {
val greetingFunction = configureGreetingFunction()
println(greetingFunction("Guyal"))
println(greetingFunction("Guyal")) } ...Read now
Unlock full access