0Pricing
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.

When to use existentials (any P) vs generics is a free Swift Academy lesson on CoddyKit — lesson 3 of 3. 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 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

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

Yes — the full text of “When to use existentials (any P) vs generics” is free to read here on the web, and the Swift Academy course includes 3 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 “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; this is — lesson 3 of 3, so you can start here or from the beginning and move at your own pace.

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