Swift Academy · Lesson

Indirect Enums and Recursive Structures

Building linked lists and trees with indirect enum cases.

Lesson 3 of 412 steps

Indirect Enums and Recursive Structures is a free Swift Academy lesson on CoddyKit. This is lesson 3 of 4. 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 4 lessons in total.

Welcome

An `indirect` enum case stores its associated value behind a pointer, enabling recursive data structures like linked lists, trees, and expression trees.

Why indirect is Needed

Without `indirect`, an enum that references itself would have infinite size: ```swift enum List { case empty case node(T, List) // ❌ recursive without size bound } ``` `indirect` adds a heap allocation so the size is just a pointer.

Defining an indirect Enum

```swift indirect enum List { case empty case node(T, List) } let list = List.node(1, .node(2, .node(3, .empty))) // [1, 2, 3] ```

Binary Tree Example

```swift indirect enum Tree { case leaf case branch(left: Tree, value: T, right: Tree) } let t = Tree.branch(left: .leaf, value: 5, right: .branch(left:.leaf, value:10, right:.leaf)) ```

Recursive Function on Indirect Enum

```swift func sum(_ list: List) -> Int { switch list { case .empty: return 0 case .node(let value, let rest): return value + sum(rest) } } print(sum(list)) // 6 ```

Tree Height

```swift func height(_ tree: Tree) -> Int { switch tree { case .leaf: return 0 case .branch(let l, _, let r): return 1 + max(height(l), height(r)) } } ```

Expression Trees

```swift indirect enum Expr { case num(Double) case add(Expr, Expr) case mul(Expr, Expr) } func eval(_ e: Expr) -> Double { switch e { case .num(let n): return n case .add(let a, let b): return eval(a) + eval(b) case .mul(let a, let b): return eval(a) * eval(b) } } print(eval(.mul(.num(2), .add(.num(3), .num(4))))) // 14 ```

Per-Case indirect

You can mark only the recursive cases `indirect` instead of the whole enum: ```swift enum Tree { case leaf indirect case branch(left: Tree, value: T, right: Tree) } ``` This is more precise — only the recursive case incurs the heap allocation.

Performance Consideration

`indirect` cases are heap-allocated. For performance-critical recursive structures, consider using a class or referencing an array-based representation instead.

When to Use indirect Enums

Good use cases: • Linked lists and functional data structures • Abstract Syntax Trees (parsers, calculators) • File system hierarchies • JSON/XML tree representations Avoid when flat array-based representations are more cache-friendly.

Quick Check

What does `indirect` add to an enum case?

Recap

Key takeaways: • `indirect enum` or `indirect case` enables recursive types • Needed because recursive value types would have infinite size • Use for linked lists, trees, and ASTs • Traverse with recursive functions and switch • Per-case `indirect` is more precise than whole-enum `indirect` Next: modelling state machines with enums.
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 “Indirect Enums and Recursive Structures” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “Indirect Enums and Recursive Structures”, 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 4 lessons in total.

What will I learn in “Indirect Enums and Recursive Structures”?

Building linked lists and trees with indirect enum cases. 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 4.

How long does the “Indirect Enums and Recursive Structures” 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. Raw Values and CaseIterable
  2. Associated Values for Rich Data
  3. Indirect Enums and Recursive Structures
  4. Modeling State Machines with Enums
← Back to Swift Academy