Argument labels in Swift
Call sites read like prose; underscores opt out.
// Argument labels read like prose at the call site; defaults trim it.
func logLeg(from start: String, to finish: String,
distanceNm: Double = 1.0) -> String {
"\(start) -> \(finish): \(distanceNm) nm"
}
// An underscore drops the label entirely.
func corrected(_ seconds: Double, handicap: Double) -> Double {
seconds * handicap
}
print(logLeg(from: "pin", to: "windward", distanceNm: 1.8))
print(logLeg(from: "windward", to: "leeward"))
print(corrected(3600, handicap: 0.92))
How it works
from start: Stringsplits the label from the parameter name.distanceNm: Double = 1.0is a default the caller may skip._ secondsdrops the first label:corrected(3600, handicap:).
Keywords and builtins used here
DoubleStringcorrectedfunclogLegprint
The run, in numbers
- Lines
- 14
- Characters to type
- 479
- Tokens
- 109
- Three-star pace
- 70 tpm
At the three-star pace of 70 tokens a minute, this run takes about 93 seconds.
Step 1 of 3 in Functions & closures, step 7 of 27 in Language basics.