October 2019
Intermediate to advanced
434 pages
11h 54m
English
We can store functions as mutable variables as well. If we wanted to make the previous example a mutable variable, we could update our variable declaration accordingly by replacing val with var as in the following code:
var greetingProvider: () -> String = { "Hello" }
Now, we can reassign the function value being stored within greetingProvider as we would with any other variable type:
fun main(args: Array<String>) { println(greetingProvider()) greetingProvider = { "Hey" } println(greetingProvider())} // output: Hello// output: Hey
In this example, we've logged String provided by the original greetingProvider variable, then assigned a new function to greetingProvider and re-logged the resultant function invocation. ...
Read now
Unlock full access