Suppose I have a function like this:
func compute(_ f: (Double, Double) -> Double,_ lhs: Double,_ rhs: Double) -> Double {
return f(lhs, rhs)
}
let result = compute(+,2,2)
print(result)
That compiles and it works as expected. It prints out 4
But then, if I delete that function 'compute' and just type this:
let result = +(2,2)
Why doesn't this work?
I mean, isn't that what basically is happening inside the 'compute' function?
Thanks.
1 Like
cukr
2
let a = 2 + 2 // this uses infix plus operator
let b = -2 // this uses prefix minus operator, and gives you negative 2
let c = +2 // this uses prefix plus operator on a number 2, and gives you 2 back
let d = +(2, 2) // this tries to use prefix plus operator on a tuple and fails, because tuple isn't a number
let e = (+)(2, 2) // this is what you want
11 Likes
oh nice. I got it, thank you!
1 Like