Rien
Rien

Reputation: 658

Function return a function that returns a function

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

Answers (2)

Airspeed Velocity
Airspeed Velocity

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

qwerty_so
qwerty_so

Reputation: 36315

Use Anylike this:

typealias nextStep = ((char: CChar) -> Any)

func process(char: CChar) -> nextStep {
  return process
}

Upvotes: 0

Related Questions