Reputation: 11
func makeIncrementer() -> (Int -> Int) {
func addOne(number: Int) -> Int {
return 1 + number
}
return addOne
}
above is a simple example code for Function as first-class type in Swift now, when i call the call the function in the following way:
var increment = makeIncrementer()
increment(7)
it perfectly gives the answer
But out of curiosity i tried the direct approach i.e.
makeIncrementer(7) // error
and it gives an error
why is it so??? P.S. I am a beginner in Swift
Upvotes: 0
Views: 140
Reputation: 154583
The call makeIncrementer()
returns the function, so to call it you pass the parameter in a second set of parentheses:
makeIncrementer()(7)
The error message is given because Swift interprets makeIncrementer(7)
as 7
being passed to makeIncrementer
which doesn't take any parameters. Hopefully Swift error messages are made more friendly in the future. While technically correct, the error message given leads to a lot of confusion.
Upvotes: 3