I have a question for auto closure. For example, we can have a function that have an auto closure like this
func logHello(ifFalse condition: Bool,
message: @autoclosure () -> String
) {
guard condition else {
return
}
print("Assertion failed: \(message())")
}
the reason why auto closure is used more of the time is because we wish to delay the execution of the closure to the body of the function, but what is the point of it?
I can have something like this
func logHello1(ifFalse condition: Bool,
message: String
) {
guard condition else {
return
}
print("Assertion failed: \(message)")
}
not using a closure at all, but just the end result what the closure might produce. Isn't this better?
I have seen aotuclosures used in assertion and other implementations of swift native functionalities, but it just puzzles me why can't we just pass in aliteral
in place of the closure?
What is the advantage of using auto closure in this case?