Here is the definition in Swift handbook:
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.
So, you see it is named closure (anyone needs a name, isn't it ?) but could as well have been called block or lambda !
I think however that closure wants to carry the idea that the block encloses the parameters it captures (that's a personal guess).
Then, in a computed property (not computer property),
var x : Int {
return 10
}
This is not a closure but the definition of the computed property.
Closure would be called with an = sign
var x : Int = { return 10 }()
see details here : https://stackoverflow.com/questions/31515805/difference-between-computed-property-and-property-set-with-closure
Some rule of thumb when to use.
- if you need a func, with parameters, return value, use func: I find it easier to read and understand
- if you need to pass a closure (like for map(), of course use a closure
This will answer most of your questions:
h ttps://medium.com/the-andela-way/closures-in-swift-8aef8abc9474