Skip to content

Latest commit

 

History

History

day-006

Day 6: Closures, Part 1

Follow along at https://www.hackingwithswift.com/100/6.

📒 Field Notes

Accepting Parameters

This caught me off guard. I initially expected a closure defined in the following way...

let greet = { (name: String) in
    print("👋 Hello, \(name)")
}

... to be called like this:

greet(name: "friend")

Alas, it appears that Swift closures can't be called with parameter labels. So we need to call it like this:

greet("friend")

There has to be a reason for this, though I'm not exactly sure what it is at the moment. Perhaps it has something to do with the compiler not being able to enforce the signature of an "anonymous" function from the caller's point of view? There could also be cases where code calling the closure can't — or shouldn't — know about parameter labels, which I can see being useful.

Trailing Closure Syntax

Like closure syntax itself, trailing closure syntax takes a bit of getting used to at first. But there's payoff. Callers have a clean, visible space to create block scopes — and we know that their not defining a new function because there's no func keyword:

func zap(response: () -> Void) {
    print("⚡️")
}

zap {
    print("Yo!")
}

Again, I'm impressed with how Swift finds ways to mix cleanliness with expressiveness. More importantly, though, this kind of syntax sets up Swift to be a great language for functional programming:

let scores = [2, 34, 4, 56, 1]

let doubledScores = scores.map { $0 * 2 }

👍

🔗 Related Links