Reputation: 658
I would like to have a function returning a function that returns a function with the same signature as the first function. I.e. the function should be able to provide itself as a return value.
Is this possible in swift?
Example (this does not compile!):
typealias nextStep = ((char: CChar) -> nextStep)
func process(char: CChar) -> nextStep {...}
Upvotes: 0
Views: 94
Reputation: 40965
Thomas's answer should work, but if you want a more typesafe alternative:
struct F {
let f: (Character) -> F
}
func f(ch: Character) -> F {
println("I've been called with an \(ch)!")
return F(f: f)
}
let g = f("a")
let h = g.f("b")
h.f("c")
Upvotes: 2
Reputation: 36315
Use Any
like this:
typealias nextStep = ((char: CChar) -> Any)
func process(char: CChar) -> nextStep {
return process
}
Upvotes: 0