Not sure if this is the best approach but I want to have functions accessible by an index and would prefer not to use a dictionary. So I was thinking of putting them into an array.
var array = [ func1, func2, ... ]
It looks like this is possible since functions are first class citizens.
BUt I'm wondering if you can do this on classes. That is, pass a function from a class instance to an array, without losing performance with extra closures.
class Foo {
var array: [Function]
init() {
array = [ f1, f2 ]
}
func f1() {
return array.length
}
func f2(a: Int, b: Int) {
// ... the functions are all different.
}
}
Wondering if anything like that is possible.
func f1()seems to return a value.f1andf2have different types. You cannot store them in a common array (only asAnywhich makes things ugly).var array: [Any]you would have to cast each element back to the correct function type in order to call the function, i.e. you have to know the signature of every element (or try all possible signatures).