typestar

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

  1. from start: String splits the label from the parameter name.
  2. distanceNm: Double = 1.0 is a default the caller may skip.
  3. _ seconds drops the first label: corrected(3600, handicap:).

Keywords and builtins used here

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.

Type this snippet

Step 1 of 3 in Functions & closures, step 7 of 27 in Language basics.

← Previous Next →