Skip to content
tezvyn:
Menu

Protocols with associated types and opaque types

MediumHow cards are made

Summary

generics and type erasure.

Key points

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.

The mistakes people make

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.

A 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.

Interview question

How does returning some Protocol differ from the goal of storing many PAT conformers in one array?

  • a.some commits to a single fixed hidden underlying type, so it does not enable a heterogeneous collection of differing conformersCorrect
  • b.some and a heterogeneous array are exactly the same feature
  • c.some only works for protocols without associated types
  • d.some erases all type info, making a heterogeneous array trivial
Why?

An opaque type fixes one concrete underlying type while hiding it, preserving type identity. It does not let you mix different conformers in one array; that needs type erasure like AnySequence.

Just read this? Test yourself on what you have been reading.

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on swift — each one lists the topics its interview covers.

See open roles