UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Question: Closure's necessity

Forums > 100 Days of SwiftUI

@boat  

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

2      

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")))

3      

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.

3      

@boat  

Thank you @vtabmow and @Obelix.

Very clear now !

Have a great day,

Boat

1      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.