According to this SO post currying in Swift with an arbitrary number of parameters isn't possible. I'm trying to understand why. Consider this code, which in my head seems like it should work:
func addArray (numbers: [Int]) -> ((Int...) -> Any) {
return { (nums: Int...) in
(nums.count > 0) ? addArray(numbers+nums) : numbers.reduce(0, combine: +) }
}
func add (numbers: Int...) -> ((Int...) -> Any) {
return addArray(numbers)
}
- Interactive REPL <-- btw, that's swift v1. Here's the same thing with swift v2 beta
First Test:
println("sum = \( add(11)() )")
// => sum = 11
so far so good...
Second Test:
println("sum = \( add(11)(12) )")
// => sum = (Function)
excellent, now can we call that function?
Third test
println("sum = \( add(11)(12)() )")
// => <stdin>:19:21: error: invalid use of '()' to call a value of
// => non-function type 'Any'
// => println("sum = \( add(11)(12)() )")
// => ^ ~~
... Huh? Didn't you just say that add(11)(12)
was a function?
Now it's a non-function type 'Any' ?!1!1 wtf?
Note that I was expecting the result to be sum = 23