In the Swift documentation Apple says this:
Closures are self-contained blocks of functionality that can be passed around and used in your code. Closures in Swift are similar to blocks in C and Objective-C and to lambdas in other programming languages.
Which I thought was the definition of First-class functions
And they also say this:
Closures can capture and store references to any constants and variables from the context in which they are defined. This is known as closing over those constants and variables. Swift handles all of the memory management of capturing for you.
I thought this was the definittion of closures while the other defitintion was for first-class functions, but Apple seems the put them together and call it closure.
Have I misunderstood something? or are Apple calling closures and first-class functions closures?
I've written this example code, and just wanna know if I'm right in the written comments?
// 'a' takes a first class function, which makes 'a' a higher order function
func a(ch: () -> Void){
print("Something")
ch() // 'ch' is a first class function
print("Ended")
}
func closureFunc(){
var num = 2
a({
// access to 'num' is possible by closures
num = num*2
print(num)
})
}
closureFunc()
self.
(has to be explicit). Take a look at #29023485 – Lennox