skip to content

Why does a Swift function value lose its argument labels, and how do you reference one specific overload by name?

level: middleimportance: should knowfreq 30%

answer

  1. labels belong to the name
  2. the type keeps only types
  3. base name plus colons
  4. annotation picks by type

basics

~20 s

Argument labels are part of a Swift function's name, not its type, so hasMinimumLength(_:of:) yields a (String, Int) -> Bool called without labels. Pick one overload by its full name when labels differ, or by a type annotation when only types differ.

solid answer

~40 s

In Swift a declaration's full name includes its labels: `hasMinimumLength(_:of:)`. Its type does not; since Swift 3 a function type keeps only parameter and return types, so the value has type `(String, Int) -> Bool`, and calling it as `check("kata", of: 3)` fails with "extraneous argument label 'of:' in call". A function type may still spell `_ attempt:` names as documentation, but they never become labels. When a base name is overloaded, `let f = hasMinimumLength` fails with "ambiguous use of 'hasMinimumLength'". Overloads whose labels differ are told apart by the full name, base name plus each label and a colon, with `_` for an unlabelled parameter. Overloads with the same labels and different types need a type annotation: `let f: (String) -> Bool = hasMinimumLength`.

code

swift · 6 lines
swift
func hasMinimumLength(_ text: String, of length: Int) -> Bool { text.count >= length }
func hasMinimumLength(_ text: String) -> Bool { text.count >= 8 }

let byFullName = hasMinimumLength(_:of:)          // (String, Int) -> Bool
let byType: (String) -> Bool = hasMinimumLength   // overload chosen by the annotation
print(byFullName("kata", 3), byType("kata"))      // true false

go deeper

for a junior

Remember that a function stored in a constant is called without labels, and that full names like hasMinimumLength(_:of:) exist to name a function precisely.

for a middle

Explain why labels belong to the name and not the type, how the underscore spelling in a function type works, and the two tools for resolving an overloaded reference.

for a senior

Anticipate how adding an overload breaks bare references elsewhere, and write references by full name or with a type annotation so an API can grow overloads safely.

for a principal

Treat full names as part of an API's surface: a team adding overloads to shared functions should know which call sites reference them as values and which ones a new overload makes ambiguous.

## Labels belong to the name, not the type Every Swift function has two things that are easy to conflate: - Its **full name**: the base name plus one label per parameter, each followed by a colon, with `_` for a parameter that has no argument label. `func hasMinimumLength(_ text: String, of length: Int) -> Bool` has the full name `hasMinimumLength(_:of:)`. - Its **type**: the parameter types and the return type, `(String, Int) -> Bool`. Swift 3 removed argument labels from function types (Swift Evolution SE-0111). Labels are how a call site reads, and they are checked when you call a declaration by name; once a function becomes a value, only its type travels with it. | Declaration | Full name | Type of the value | |---|---|---| | `func hasMinimumLength(_ text: String, of length: Int) -> Bool` | `hasMinimumLength(_:of:)` | `(String, Int) -> Bool` | | `func hasMinimumLength(_ text: String) -> Bool` | `hasMinimumLength(_:)` | `(String) -> Bool` | | `func isEven(n: Int) -> Bool` | `isEven(n:)` | `(Int) -> Bool` | ## Why labels left the type If labels were part of the type, `(left: Int, right: Int) -> Void` and `(top: Int, bottom: Int) -> Void` would be different types, and a variable holding one could not hold the other. Because they are not, the Swift reference manual's own example works: 1. `var f = someFunction` where `someFunction(left:right:)` takes two `Int`s gives `f` the type `(Int, Int) -> Void`. 2. `f = functionWithDifferentLabels` with labels `top:bottom:` is accepted, since only the types matter. 3. Assigning a function that takes `(Int, String)` is rejected: the types differ. This is what lets any function of the right shape fill a callback slot, whatever its author named the parameters. ## Calling through a value Because the value's type has no labels, the call through it has none either: ```swift let check = hasMinimumLength(_:of:) check("kata", 3) // fine check("kata", of: 3) // error: extraneous argument label 'of:' in call ``` A function type may name its parameters for readability, `(_ attempt: Int, _ statusCode: Int) -> Bool`, and the underscore is mandatory: `(attempt: Int, statusCode: Int) -> Bool` is rejected with "function types cannot have argument labels; use '_' before 'attempt'". Those names show up in documentation and code completion, but calls through the value remain unlabelled. The reverse also holds: a labelled function such as `isEven(n:)` fits an `(Int) -> Bool` parameter, because the slot only checks types. ## Picking one overload A bare base name works when exactly one function has it. When the name is overloaded, Swift needs more information, and there are two tools: 1. **The full name** separates overloads whose labels differ. `hasMinimumLength(_:of:)` and `hasMinimumLength(_:)` are different names, so each reference is unambiguous. 2. **A type annotation** separates overloads that share labels but differ in types. Two methods `overloadedMethod(x:y:)` taking `(Int, Int)` and `(Int, Bool)` stay ambiguous by full name; `let d: (Int, Bool) -> Void = instance.overloadedMethod(x:y:)` resolves it. The context can also be a parameter type, which is why `sorted(by: <)` picks `String`'s `<` without any annotation. Without either, the compiler reports "ambiguous use of" the name and lists each candidate. ## Operators and initializers as values The same rules apply to the other named callables: - An **operator** is referenced by its symbol. In argument position the context supplies its type: `names.sorted(by: >)`. Standing alone it needs parentheses and a type: `let ascending: (Int, Int) -> Bool = (<)`; without the annotation `(<)` is ambiguous across all the `<` overloads. - An **initializer** is referenced through its type with its labels: for a struct `RetryPolicy` whose only stored property is `shouldRetry`, `RetryPolicy.init(shouldRetry:)` is a function from that property's type to `RetryPolicy`. ## Mistakes that show up in review - Expecting labels at the call site of a stored function, then "fixing" the compile error by renaming the property. - Writing a labelled function type and getting the "function types cannot have argument labels" error. - Adding an overload to a widely referenced function name and breaking every bare reference to it with an ambiguity error; references by full name or with a type annotation survive. - Believing the full name always disambiguates; it does not when two overloads share every label.

  • Two overloads share the full name hasMinimumLength(_:of:), one taking an Int length and one a Double. How do you store one of them?
    The full name alone stays ambiguous because both declarations have the same labels. Supply the type: `let pick: (String, Int) -> Bool = hasMinimumLength(_:of:)`, or pass the reference where a parameter's function type already pins it. Overloads that differ only by types are told apart by type context, not by name.
  • Why might naming parameters in a stored closure's type still be worth it?
    Writing `var shouldRetry: (_ attempt: Int, _ statusCode: Int) -> Bool` documents which `Int` is which, and the names appear in code completion. They are not argument labels, so calls stay `shouldRetry(2, 503)`; the underscore is required, and leaving it out is a compile error.

  • A stored function is called with its original labels, as in check("kata", of: 3).
  • A function type can be written (attempt: Int, status: Int) -> Bool with labels.
  • The full name always resolves an overload, even when the labels are identical.
  • A function with a labelled parameter cannot be passed where (Int) -> Bool is expected.
  • Operators cannot be stored in a constant, only passed straight to a call.