WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

SOLVED: Question: Closure's necessity

Forums > 100 Days of SwiftUI

Please see the following codes that I copied from Paul's Unwrap app's closure section.

let drivingWithReturn = { (place: String) -> String in 
      return "I'm going to \(place) in my car"
}

let message = drivingWithReturn ("London")

print(message)

My Question:

Why do we need to make a constant message ?
Why don't we just call the closure when we want to use it ? like: print (drivingWithReturn("London")

I mean, drivingWithReturn is already a constant, right ? I thought the whole point of closures is to cut the medium/agency and pass around a functionality easily.

But if we need to assign drivingWithReturn to other constants, why don't we just use a func.

Please enlighten me and thanks in advance,

Boat

1      

You are correct. You don't need to make it a constant. Paul is demonstrating that you can make it a constant. Sometimes he will demonstrate some functionality when there's actually a quicker way to do it to let us know the other possibilities. Once the closure is in a constant you use it like a constant.

let drivingWithReturn = { (place: String) -> String in
      return "I'm going to \(place) in my car"
}

let message = drivingWithReturn ("London")

print(message)
print(drivingWithReturn("London"))

// using message in a new constant
let newMessage = "Tomorrow, I have to go to the city. \(message)."
print(newMessage)

// or passing message into a function by first creating a new constant and a new function
let newMessage2 = "Tomorrow, I have to go to the city"
func combineMessages(firstMessage: String, secondMessage: String ) -> String {
    "\(firstMessage). \(secondMessage)."
}

// Then this works
print(combineMessages(firstMessage: newMessage2, secondMessage: message))

// and this works, but the one above looks cleaner
print(combineMessages(firstMessage: newMessage2, secondMessage: drivingWithReturn("London")))

// Of course you can skip creating the newMessage2 constant, but it starts to get messy and the more parameters you are passing the messier it gets.
print(combineMessages(firstMessage: "Tomorrow, I have to go to the city", secondMessage: drivingWithReturn("London")))

2      

Vince has the right answer. You don't need to! Much of what @twostraws uses in his lessons are instructions to help new programmers build up their knowledge and confidence.

You're beyond much of this and can clearly see better ways to accomplish what @twostraws shows in his sample code.

let two = 2 // assign an integer to a contstant var
let three = 3 // assign an integer to a constant var

let answer = two + three // why use variables?
let anotherAnswer = 2 + 3 // why not just insert the integers?

A hard concept for some new programmers is accepting the fact that you can assign an entire function to a variable! Just like you can assign an integer to a variable, you can also assign an entire function to a variable.

// define a variable containing a function (not a String, not an Int, a FUNCTION)
let drivingWithReturn = { (place: String) -> String in
      return "I'm going to \(place) in my car"
}

let message = drivingWithReturn ("London")  // what is message? it a variable! what does it hold? a FUNCTION!

So, just like you might substitute the var two in the place of the integer 2, you can also substitute the variable message anyplace where you could also insert a function that consumes a String and returns a String.

Go wild! Your choice.

2      

Thank you @vtabmow and @Obelix.

Very clear now !

Have a great day,

Boat

   

Hacking with Swift is sponsored by Emerge

SPONSORED Optimize your app’s startup time, binary size, and overall performance using Emerge’s advanced app optimization and monitoring tools. Reliably measure app size, speed up your app's startup time with Emerge's Launch Booster, and much more. Emerge is actively used by many of the top mobile development teams in the world.

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.