Protocols with associated types and opaque types
generics and type erasure.
a PAT leaves a placeholder type unresolved, so it has no single concrete shape and historically could not be used as a bare existential; opaque types with some let you return one fixed underlying conforming…
WHY IT EXISTS Protocols often need to refer to a type that varies per conformer, such as the Element a Container holds. An associated type is a named placeholder the conforming type fills in, letting one protocol describe a family of related shapes.
THE MENTAL MODEL With associatedtype Element, each conformer chooses its own Element. So Protocol with an associated type is not one type; it is a template whose full signature is only known once the associated types are pinned. That is why it behaves like a generic constraint rather than a concrete type.
THE LIMITATION Because the associated types are unresolved, the compiler cannot give the protocol a single fixed memory layout or method signature, so historically you could not write let delegates: [MyPAT] or use it as a plain existential; you got the can only be used as a generic constraint error. You instead used it to constrain generics, like func use<T: MyPAT>(_ x: T), or built a type-erased wrapper such as AnySequence to box differing conformers behind a uniform interface.
HOW OPAQUE TYPES HELP Returning some Protocol promises the caller a single, specific, but hidden underlying type that conforms. Unlike an existential, an opaque type preserves type identity, so associated types line up and the compiler can reason about them. This lets a function expose a PAT-conforming result, like some Collection, without naming the concrete type, while keeping full type information internally.
WHEN IT MATTERS Opaque return types power SwiftUI's some View and let library authors hide implementation types. Note newer Swift relaxes some existential restrictions with the any keyword, but opaque types remain the tool when you need a single consistent underlying type.
COMMON WRONG ANSWERS Thinking some lets you store many different PAT conformers in one heterogeneous array; it pins exactly one underlying type. Confusing some with any. Believing a PAT is just a normal protocol.
ONE CONCRETE EXAMPLE You cannot write var shapes: [Drawable] if Drawable has an associated type. A function makeContent() -> some View returns one concrete underlying view type chosen at compile time, hidden from the caller, so SwiftUI keeps full type info and the associated types resolve, all without you spelling out the sprawling concrete type.
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.