belle tian belle tian - 4 years ago 84
Swift Question

Type of "Int -> Bool" ,"Int-> Bool -> Int","Int-> String -> Int-> Bool"

There is a func:

func (first: Int) -> Int -> Bool -> String {

return ?
}


How to write the return value?
I'm so confused about the return type like of the func above.

Answer Source

Read from right to left when it comes into parsing function/closure returns. The right outermost one is the return type, and you can place the rest into parentheses.

Thus, your function declaration is equivalent to

func (first: Int) -> ((Int) -> ((Bool) -> String))

This is similar to function currying. You function returns a function that takes an Int as argument and returns another function that takes a Bool as parameter and returns a String. This way you can easily chain function calls.

So the body of your method must return the first function in the list, one that has the following signature:

func ((Int) -> ((Bool) -> String))

You can then call it like this:

f(1)(2)(true)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download