0Pricing
Swift Academy · Lesson

Indirect Enums and Recursive Structures

Building linked lists and trees with indirect enum cases.

Indirect Enums and Recursive Structures is a free Swift Academy lesson on CoddyKit — lesson 3 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.

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.

Frequently asked questions

Is the “Indirect Enums and Recursive Structures” lesson free?

Yes — the full text of “Indirect Enums and Recursive Structures” 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 “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; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

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