swift argument labels and keywords

Viewed 140

I am trying to learn Swift and came across the argument labels and an online example as follows:

func setAge(for person: String, to value: Int) {
    print("\(person) is now \(value)")
}

This can be then called as:

setAge(for: "Paul", to: 40)

My question is that isn't for a Swift keyword? I am wondering whether this use of for has some hidden meaning that I am missing or just that these keywords can also be used as argument labels?

2 Answers

or just that these keywords can also be used as argument labels?

Exactly. This is introduced in SE-0001 Allow (most) keywords as argument labels. The motivation is that:

Sometimes, the most natural label for an argument coincides with a language keyword, such as in, repeat, or defer. Such keywords should be allowed as argument labels, allowing better expression of these interfaces.

Though in SE-0001 it is said that inout, var and let are not allowed to be argument labels (back then they are used to describe the mutability of the parameter). This restriction was later relaxed. Now, only inout is not allowed as an argument label, and you get a warning if you use var or let as an argument label.

Yes this keyword can also be used as argument label. Swift gives you flexibility to use any keyword except inout as argument label as it increases readability.

Related