What Does a Closure Close? And What's the Difference of a Closure to a Computer Property? Why and When Do We Prefer a Closure Over a Function or a Computer Property?

Hi. What does a closure or a named closure (aka a function) close?


What's the different between a closure and a computed property if both are called when declaring a variable? What are the signs one needs to use a computed property, a closure or a function (much like one know that a class has to be used if he wants things inherited or a structure if he wanst things that don't inherit and wants to values to change?


Thank you.

God bless, Proverbs 31

Accepted Reply

The term closure has a long history in this space. The name is derived from the fact that it ‘closes over’ the values in its scope. For example, in this code:

func makeAdder(value: Int) -> (Int) -> Int {
    return { x in
        return x + value
    }
}

let f = makeAdder(value: 5)
print(f(3))

the closure returned by

makeAdder(value:)
captures
value
, so that it can use it after
makeAdder(value:)
has returned.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Replies

Your question does not make sense for me...



It is very hard for me to see what you really want to know.


Please show some example code to explain what sort of code you have in mind.

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

The term closure has a long history in this space. The name is derived from the fact that it ‘closes over’ the values in its scope. For example, in this code:

func makeAdder(value: Int) -> (Int) -> Int {
    return { x in
        return x + value
    }
}

let f = makeAdder(value: 5)
print(f(3))

the closure returned by

makeAdder(value:)
captures
value
, so that it can use it after
makeAdder(value:)
has returned.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thanks all.


Does the closure include the { and } braces + the code inside those braces or just the code inside those braces?

Also why can't you used closures in a class or struct property and vice-versa- why can't you use a computed property

in regular stored properties that are outside a class or a struct?

Hi.


I always come across the concept of "pass", what does that concept in Swift mean?


func whatIsTheMeaningOfPass(pass: Int) -> Void {
     print("This function is working...\(pass)")
     return     // by the way, can this be "return nil" too, since void and nil are both nothing?
}

whatIsTheMeaningOfPass = (pass: 123)


Correct me if I'm wrong, "pass" means "pass an argument" which is 123 the same as "put the value" 123 into func's paramenter which is pass: Int so that print can interpolate pass?


How about for "pass a closure" (as mentioned by Claude- thanks again) like in

var inClosures : Int = { return 10 }()   // why's there a void () at the end of the closure btw?


so for functions with a closure as parameter:

func whatIsTheMeaningOfPass(inClosures: () -> ()) {
     print("This function is a perfect \(inClosures)")
}

To follow the data flow, in narrative (correct me if I'm wrong), one could say: "pass a closure" or "put in the value" of the variable inClosures into func whatIsTheMeaningOfPass's parameter, which is inClosures: () -> (), so that print can interpolate inClosures?


Thanks again in advance.

(brain/mind's drained at the moment but perseverance is key with programming for sure (beginner- feeling more like a nursery, but perseverance is a virtue:-))

Hi. I've coma across that term phrase, "block of code" and "lambda"; since closures function like lambda and block, could it be called "closure of code" instead of block of code then? Thanks.

Correct me if I'm wrong: what value is x in the closure? Is it 3? So in x + 5, 5 has to wait

until you call print and add the argument 3, for print to finally print 8?


Closures- it's powerful but very mindblowing (like it inside outs things:).