Swift Academy · Lesson

When to use existentials (any P) vs generics

Choose between any P (existential types) and generics : heterogeneous storage & dynamic behavior vs compile-time specialization & static guarantees.

Lesson 3 of 38 steps

When to use existentials (any P) vs generics is a free Swift Academy lesson on CoddyKit. This is lesson 3 of 3. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 3 lessons in total.

Existentials vs generics

Existentials (any P) store values of unknown concrete type that conform to a protocol. Generics (<T: P>) keep the concrete type at compile time for specialization.

Protocol setup

Protocols without associated types are easy to use as any P existentials and as generic constraints.

protocol Animal {
    func speak() -> String
}
struct Dog: Animal { func speak() -> String { "woof" } }
struct Cat: Animal { func speak() -> String { "meow" } }

Heterogeneous arrays

Use [any Animal] to store different conformers together and call only the protocol API.

let zoo: [any Animal] = [Dog(), Cat()]   // heterogeneous array
for a in zoo { print(a.speak()) }              // "woof", "meow"

Specialization via generics

Generics preserve the concrete type (T) enabling more optimization and stronger compile-time checks.

// Generic version — compiler specializes for each concrete type
func shout<T: Animal>(_ a: T) {
    // Can inline/specialize based on concrete T
    print(a.speak().uppercased())
}
shout(Dog()) // WOOF
shout(Cat()) // MEOW

PAT limitations

Protocols with associated types don’t work well as plain existentials if you need the associated type. Prefer generics or type erasure (e.g., AnySequence).

protocol Container {
    associatedtype Item
    func get(_ i: Int) -> Item
}
// Storing as `any Container` loses the concrete Item.
// You can't use Item directly without additional constraints or type erasure.

When to pick which

Choose:

  • any P when you need heterogeneous storage or dynamic behavior via protocol API only.
  • Generics when you want static types, better optimization, or must relate multiple types (e.g., same Item).
  • For PATs where Item matters, use generics or add type erasure.

any P vs generics

Quick check: When is any P preferred?

Recap

Recap: Use any P for mixed conformers and dynamic behavior; use generics for compile-time specialization and when associated types/relationships matter.

Free to start

Learn Swift with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
122
Lessons
409

Frequently Asked Questions

Is the “When to use existentials (any P) vs generics” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “When to use existentials (any P) vs generics”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 3 lessons in total.

What will I learn in “When to use existentials (any P) vs generics”?

Choose between any P (existential types) and generics : heterogeneous storage & dynamic behavior vs compile-time specialization & static guarantees. 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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 3.

How long does the “When to use existentials (any P) vs generics” 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. associatedtype & generic protocols
  2. Type erasure patterns (AnySequence/AnyIterator)
  3. When to use existentials (any P) vs generics
← Back to Swift Academy