Reputation: 125
How do you define an array of funs(functions) in Kotlin?
This is my assumption of how it would be done...
this.funs = arrayOf : Unit(
this::openCamera,
this::sendSMS,
this::makeCall
)
How would I invoke each array item as a function at runtime? Would simply specifying 'funs' in place of either openCamera(), sendSMS(), and MakeCall() in a forEach() loop work?
or.. is this the way that I would invoke each function:
val function = getFunction(funs)
function()
Upvotes: 2
Views: 614
Reputation: 8315
If you had some functions defined as
fun one() { print("ONE") }
fun two() { print("TWO") }
fun three() { print("THREE") }
Then you can create an array of these as
fun someFun(){
val arrayOfFunctions: Array<() -> Unit> = arrayOf(::one, ::two, ::three)
arrayOfFunctions.forEach { function ->
function()
}
}
Upvotes: 5