skip to content

As the lead of a Swift codebase, what default would you set between `some`, `any` and hand-written type erasure in API signatures, and when would you break it?

level: principalimportance: should knowfreq 36%

answer

  1. default to the static choice
  2. any where values must mix
  3. erasers only when conformance is needed
  4. ExistentialAny with -Werror

basics

~20 s

Default to some or a named generic, which keeps concrete types known to the compiler. Use any where different types must share storage or a dependency is swapped at run time; write an eraser only when the erased value must conform.

solid answer

~40 s

My default is `some` or a named generic in parameters and returns: the caller keeps its type, generic code can be specialized, and an opaque return lets a resilient library change its hidden type later, unless the function is `@inlinable`. I accept `any` where it is honest: heterogeneous collections, plug-in registries, and dependencies in long-lived objects, where `any PaymentGateway` keeps the owner non-generic and the box cost is negligible beside network work. Hand-written erasers need a reason that `any P<T>` or a generic helper cannot meet: the value must conform, needs extra conformances, or the deployment target is too old. To make the rule stick, enable the `ExistentialAny` upcoming feature, which Swift 6 mode does not turn on, and escalate its warnings with `-Werror ExistentialAny`.

code

bash · 1 line
bash
swiftc -swift-version 6 -enable-upcoming-feature ExistentialAny -Werror ExistentialAny -typecheck Payments.swift

go deeper

for a junior

Recall that some keeps one concrete type the compiler knows and any boxes values of different types, and that most codebases prefer some unless mixing is needed.

for a middle

Explain the mechanics behind the default: some parameters are generic, some returns hide one type, and any adds a box with restricted member access.

for a senior

Show where each choice fits in real code: heterogeneous storage and injected services for any, inner loops and public returns for some, erasers only for conformance.

for a principal

Set and enforce the default: write the rule, enable ExistentialAny with -Werror where the team wants it, require a reason for each public any, and revisit as deployment targets rise.

## The decision the team owns Every protocol-typed signature in a Swift codebase is a choice between three tools: **`some` or a named generic** (the concrete type stays known to the compiler), **`any`** (a box whose type is found at run time) and a **hand-written type eraser** (a concrete wrapper type you maintain). No answer is correct everywhere, which is why it is worth one written default that reviewers can point to. ## Default: `some` and named generics - **Parameters.** `some PaymentProvider`, or `<P: PaymentProvider>` when the type needs a name, keeps the caller's concrete type. The full protocol is usable in the body, and the optimizer can specialize the function. - **Returns.** `-> some PaymentProvider` hides the type but keeps its identity. In a library built for evolution, SE-0244 notes the underlying type can change between versions without breaking ABI. The exception is an `@inlinable` function, whose underlying type must be `public` or `@usableFromInline` and cannot change compatibly. - **Readability.** A short `some` parameter reads like the protocol name. Switch to a named generic once constraints grow, because the `some` shorthand cannot carry a `where` clause or tie two parameters to one type. - **The price.** Generics spread. A type that stores a generic dependency becomes generic itself, `CheckoutModel<Gateway: PaymentGateway>`, and that parameter follows it into every type that holds it. ## Where `any` is the honest choice - **Heterogeneous storage:** `[any PaymentProvider]`, plug-in registries, anything whose concrete types are only known at run time. - **Long-lived dependencies:** a view model or service holding `let gateway: any PaymentGateway` stays non-generic, tests can inject a fake, and the box's cost disappears next to a network round trip. - **Boundaries where a generic parameter would leak** an implementation choice into many unrelated signatures. The existential cost (indirection, a separate allocation for values too large for the box's inline buffer, and no specialization) matters in hot loops over many values. Measure it with the optimization tooling rather than guessing. ## Hand-written erasers: the exception Since Swift 5.7, `any Parser<Int>` covers most old reasons for an `AnyParser`. Keep an eraser only when: - the erased value must itself conform, for example as a generic argument, because an existential never conforms to its own protocol; - you need conformances or behaviour the existential cannot carry, such as `Sendable` or logging; - the Apple deployment target is older than the run-time support for parameterized protocol types, which Swift 6.4 reports as iOS 16 for an iOS target. Each eraser is code to forward and keep in step with its protocol, so ask for the reason in review. ## Making the rule enforceable 1. **Spell existentials explicitly.** The `ExistentialAny` upcoming feature belongs to a future language mode, so adopting Swift 6 mode does not turn it on. Enable it with `-enable-upcoming-feature ExistentialAny`. 2. **Decide how loud it is.** Since Swift 6.1 the compiler reports a missing `any` as a warning, including under `ExistentialAny`; `-Werror ExistentialAny` turns that diagnostic group into errors. 3. **Migrate mechanically, then review.** `-enable-upcoming-feature ExistentialAny:migrate` adds fix-its that prepend `any` and never converts to `some`, so a reviewer then looks for parameters that should have been `some`. 4. **Write the review rule down:** every `any` in a public signature carries a one-line reason. | Choice | Use it when | What it costs | |---|---|---| | `some` / named generic | one type per call or per declaration | generic parameters spread; one return type per declaration | | `any P` / `any P<T>` | values of different types share storage | box indirection; no conformance; reduced member access | | Hand-written eraser | the erased value must conform or carry extra behaviour | forwarding code and maintenance | ## Where judgment still varies Teams disagree at the edges, and that is legitimate. An app team may prefer `any` for every injected service, accepting the box for simpler types. A library team shipping a binary framework may push `some` returns harder for evolution. A performance-sensitive module may ban `any` in inner loops entirely. The lead's job is to choose one default, record why, and make exceptions visible.

  • A view model stores `let gateway: any PaymentGateway`; a reviewer asks for a generic parameter instead. How do you decide?
    Ask what the generic buys. If the gateway is called a few times per user action, the box cost is irrelevant beside network work, and a generic parameter would spread `<Gateway: PaymentGateway>` into every type holding the view model. Keep `any`. Choose a generic when the dependency sits in a hot path, or when callers need its concrete type or associated types.
  • Why can a public `-> some PaymentProvider` be safer for a binary framework than returning the concrete type?
    Clients never see the concrete type, so a later version can return a different conforming type without breaking ABI, as SE-0244 describes. Returning the concrete type freezes it into the public interface. The exception is an `@inlinable` function, whose inlined body exposes the underlying type, so that type cannot change compatibly.

  • Swift 6 language mode already requires any on every existential type.
  • Use any everywhere because it is simpler and costs nothing.
  • A some return in an @inlinable function can change its hidden type freely.
  • Every protocol with an associated type needs a hand-written AnyX eraser.
  • ExistentialAny's migration fix-its convert parameters to some where possible.

Assumes Swift 6.4: ExistentialAny belongs to a future language mode (off in Swift 5 and Swift 6 modes), and since Swift 6.1 missing-any diagnostics are warnings that -Werror ExistentialAny escalates.