As the lead of a multi-module Swift app, what rules would you set for retroactive conformances and public protocol extensions across modules?
answer
- who owns each conformance
- app target versus shared module
- requirement or extension-only, on purpose
- same package hides the warning
basics
~20 sGive each conformance one owner: the type's module or the protocol's. Keep unavoidable retroactive conformances in the app target, marked and justified, prefer wrapper types, and choose deliberately which protocol members are requirements and who may extend shared protocols.
solid answer
~50 sI would set three rules. **Ownership:** a conformance lives in the module that owns the type or the protocol; a retroactive one, such as `Date: Identifiable`, is allowed only in the app target, marked `@retroactive` with a comment, and a wrapper type is the default answer. The compiler will not enforce this everywhere, since modules in one package are exempt from the Swift 6 warning, so code review and a lint rule must. **Customization points:** in a public protocol, anything conformers are meant to replace is declared as a requirement; extension-only helpers get names that say they are fixed. **Extension hygiene:** shared modules avoid broad public extensions on standard-library protocols, because two modules adding the same `Collection` method make every call site that imports both ambiguous. The tradeoff is some boilerplate in exchange for behaviour that does not change on an SDK update.
go deeper
Recall that conforming a type you do not own to a protocol you do not own is the risky case, and that @retroactive only marks it.
Explain how module boundaries shape the rules: where a conformance is declared, and why requirements and extension-only members behave differently.
Bring the failure stories: a conformance duplicated by an SDK update, an ambiguous helper after a new import, a helper promoted to a requirement.
Own the policy and its enforcement: which module may declare which conformance, how review and linting cover what the compiler exempts, and what boilerplate you accept for stability.
## The decision In a Swift app split into modules (feature modules, a design system, networking, shared utilities), two features of protocol-oriented code cross module lines constantly: **conformances** (`extension Date: Identifiable`) and **protocol extensions** (`extension Collection { public func chunked() ... }`). Each is cheap to write and expensive to undo. A lead's job is to decide which module may declare what, and how that is enforced when the compiler does not. There is no single right policy; the tradeoff is boilerplate now (wrapper types, extra requirements, longer names) against behaviour that can change on an SDK update or a new import. ## Rule 1: every conformance has one owner A **conformance** is unique within a process. The safe places to declare one are: - the module that owns the **type** (only it can know the type's invariants), or - the module that owns the **protocol** (it defines what conforming means). A **retroactive** conformance, where neither is yours, is the exception. Since the Swift 6.0 compiler it draws a warning, silenced with `@retroactive`; the attribute changes nothing at run time. A workable policy: 1. Default to a **wrapper type** or a protocol you own. 2. If a retroactive conformance is unavoidable, it lives **only in the app target**, which nothing imports, marked `@retroactive` with a comment explaining why. 3. Shared modules never declare one, because it would reach every importer. ## Rule 2: the compiler does not cover your own modules The warning applies only when both the type and the protocol come from other modules **outside your package**. An amendment to SE-0364 exempts modules in the same package, and the Swift 6.4 compiler honours it: when `Event` and `Sink` were built as two modules with the same package name, a third module in that package conforming one to the other compiled silently, and the warning appeared only under a different package name. In a codebase organised as one Swift package with many targets, conformances between your own modules are invisible to the warning. Review checklists or a lint rule have to state who may conform which shared type to which shared protocol. ## Rule 3: requirements are public promises For each public protocol, decide on purpose which members are **requirements** and which are **extension-only**: | Member kind | Through the protocol | Use it for | |---|---|---| | Requirement with a default | Each conformer's version runs | Customization points | | Extension-only member | The extension's version always runs | Fixed helpers built on requirements | Two consequences belong in the policy: - A conformer can shadow an extension-only helper and believe it customized it. Names should make fixed helpers obviously fixed, and tests should call through the protocol. - **Promoting** a helper to a requirement changes behaviour for every conformer that already had a same-named method, because those shadows start running through the protocol. Treat it as a breaking change, announced to the module's clients. For libraries built with library evolution, Swift's library-evolution document permits a new non-type requirement only with an unconstrained default implementation and suitable availability. ## Rule 4: one owner for extensions on shared protocols Public extensions on standard-library protocols (`Collection`, `Sequence`, `StringProtocol`) are global in effect. When two modules each add `public func chunked()` to `Collection`, each compiles on its own, but every file importing both gets "ambiguous use of 'chunked()'". Options: - a single utilities module owns all such extensions; - feature modules keep their extensions `internal`; - names carry a domain-specific word so collisions are unlikely. ## Enforcement and its cost - **Code review**: a checklist line for any new `extension X: P` where neither `X` nor `P` is declared in the module. - **Linting**: flag `@retroactive` outside the app target, and public extensions on standard-library protocols outside the utilities module. - **Documentation**: each public protocol states its customization points. The cost is real: wrapper types add conversions, and a strict owner for extensions slows small conveniences down. The benefit is that a Foundation update or a new import cannot silently change which code runs, and that is the argument to make when the team pushes back.
- Your shared Swift module wants to promote an extension-only helper to a protocol requirement. What do you check first?Which conformers already have a member with that signature. Today their versions are only used on the concrete type; once the helper is a requirement, those methods start running through the protocol and from inside the defaults, which can change behaviour in modules you do not see. For a library built with library evolution, a new non-type requirement is only allowed with an unconstrained default and suitable availability.
- Two Swift modules in the app each add a public chunked() method to Collection. What breaks?Any file that imports both modules gets "ambiguous use of 'chunked()'" at each call, even though each module compiles on its own. Fixing it means renaming one, moving the helper into one shared utility module, or restricting it with access control. The rule I would set is one owner for extensions on standard-library protocols.
- @retroactive makes a cross-module conformance safe, so allow it anywhere.
- The compiler warning will catch every retroactive conformance in our codebase.
- Public extensions on Collection in shared modules never conflict with each other.
- Turning a helper into a requirement can never change existing conformers' behaviour.
Assumes the Swift 6.0 or later compiler (checked on 6.4): the retroactive-conformance warning and its same-package exemption come from SE-0364 and its amendment, shipped in 6.0.