0Pricing
Swift Academy · Lesson

Primary Associated Types and Typed Throws

Constraining protocol existentials and Swift 6 typed error propagation.

Primary Associated Types and Typed Throws is a free Swift Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Primary Associated Types

Swift 5.7 lets you specify primary associated types on protocols, enabling constrained existentials.

protocol Container<Element> {
  associatedtype Element
  var items: [Element] { get }
}
struct Bag<T>: Container { var items: [T] = [] }

Constrained Existentials

With primary associated types you can write any Container to narrow the existential.

func printItems(_ c: any Container<Int>) {
  c.items.forEach { print($0) }
}
printItems(Bag(items: [1,2,3]))

Collection Constrained Existentials

Standard library protocols like Collection now support primary associated types.

func first(_ c: any Collection<String>) -> String? {
  c.first
}
print(first(["a","b","c"]) ?? "none")

Typed Throws Introduction

Swift 6 introduces typed throws so you can declare the exact error type a function may throw.

enum NetworkError: Error { case timeout, notFound }
func fetch() throws(NetworkError) -> Data {
  throw NetworkError.timeout
}

Exhaustive catch with Typed Throws

Because the error type is known, the compiler can verify that all cases are handled exhaustively.

do {
  let _ = try fetch()
} catch .timeout {
  print("Timed out")
} catch .notFound {
  print("Not found")
}

Typed Throws in Protocols

Protocol requirements can specify a typed throw, letting conformers provide precise error types.

protocol Fetchable {
  associatedtype Failure: Error
  func fetch() throws(Failure) -> Data
}

Rethrowing with Typed Throws

rethrows works with typed throws: the outer function propagates the inner error type.

func map<T, E: Error>(_ array: [T], transform: (T) throws(E) -> T) throws(E) -> [T] {
  try array.map { try transform($0) }
}

Combining Primary AT with Typed Throws

You can use both features together for precise, type-safe APIs.

protocol Repository<Model> {
  associatedtype Model
  associatedtype Failure: Error
  func fetchAll() throws(Failure) -> [Model]
}

Opaque Typed Throws

throws(any Error) is the equivalent of untyped throws, giving you the escape hatch.

func riskyOp() throws(any Error) {
  // can throw any error
}

Migration: Untyped to Typed Throws

Migration path: annotate existing throwing functions with the concrete error type step-by-step.

// Before:
func load() throws -> Data { ... }
// After:
func load() throws(NetworkError) -> Data { ... }

Typed Throws and async

Typed throws compose naturally with async: async throws(E) is valid syntax.

func fetchAsync() async throws(NetworkError) -> Data {
  throw NetworkError.notFound
}

Quick Check

What does a primary associated type on a protocol enable?

Lesson Recap

Primary associated types (Swift 5.7) enable constrained existentials like any Collection. Typed throws (Swift 6) let you declare exact error types for exhaustive catch blocks. Both features improve type safety and API expressiveness.

Frequently asked questions

Is the “Primary Associated Types and Typed Throws” lesson free?

Yes — the full text of “Primary Associated Types and Typed Throws” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.

What will I learn in “Primary Associated Types and Typed Throws”?

Constraining protocol existentials and Swift 6 typed error propagation. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Primary Associated Types and Typed Throws” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Generic Constraints and where Clauses
  2. Opaque Types with some Keyword
  3. Existentials with any and Type Erasure
  4. Primary Associated Types and Typed Throws
← Back to Swift Academy