Quick, subtle difference between career and higher order feature

NOTE. This question was asked, and Swift 2.1 was the last.

Given:

class IntWrapper {
    var i: Int = 1
}
func add(inout m: Int, i: Int) {
    m += i
}

And a higher order function

func apply() -> (inout i: Int) -> () -> () {
    return { (inout i: Int) -> () -> () in
        return {
            add(&i, i: 1)
        }
    }
}

An application as such results in a change in the value of a member variable:

var intW = IntWrapper()
print(intW.i)                             // Prints '1'
apply()(i: &intW.i)()
print(intW.i)                             // Prints '1'

However, when the function changes to curry form

func apply()(inout i: Int)() -> () {
    add(&i, i: 1)
}

It is used when changing the value of a member variable:

var intW = IntWrapper()
print(intW.i)                             // Prints '1'
apply()(i: &intW.i)()
print(intW.i)                             // Prints '2'

I am curious why this is so. I always thought that cardiac syntax was sugar for a higher ordered form of function, but there seems to be semantic differences.

, swift, , , , , , , . ?

Higher order functionCurrying

+1

Source: https://habr.com/ru/post/1619059/


All Articles