The problem I have faced is that I need to make a sequence of functions that can be invoked, but faced a problem that is, even though a function is a first-class member and do conform to protocol Any, the code bellow does not work.
struct FunctionSequence {
var callbacks = [Any]() //how to restrict Any to only functions?
init(with functions: Any...){
self.callbacks = functions
}
func callAll(){
for f in callbacks {
f()
}
}
}
Compilation interrupts with:
error: cannot call value of non-function type 'Any'
So I ask people who's deep in the swift for some help. PS The result I need is as following:
var printer = FunctionSequence
.init(with: {print("Hello,")}, {print("world!")})
printer.callbacks.insert({print("I hate you,")}, at: 1)
printer.callAll()
//outputs "Hello, I hate you, world!"