# What are some examples where enumerations with indirection are preferred to a class?

**URL:** <https://forums.swift.org/t/what-are-some-examples-where-enumerations-with-indirection-are-preferred-to-a-class/56784>\
**Category:** Using Swift\
**Created:** [April 17, 2022, 12:48am UTC](https://forums.swift.org/t/what-are-some-examples-where-enumerations-with-indirection-are-preferred-to-a-class/56784 "2022-04-17T00:48:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Austin\_Conlon](https://avatars.discourse-cdn.com/v4/letter/a/edb3f5/32.png) [@Austin\_Conlon](https://forums.swift.org/u/Austin_Conlon)\
**Post date:** [April 17, 2022, 12:48am UTC](https://forums.swift.org/t/what-are-some-examples-where-enumerations-with-indirection-are-preferred-to-a-class/56784/1 "2022-04-17T00:48:33Z")

</div>

[The Swift Programming Language](https://docs.swift.org/swift-book/ReferenceManual/Declarations.html#ID364) shows a brief example of a recursive tree structure

```swift
enum Tree<T> {
    case empty
    indirect case node(value: T, left: Tree, right: Tree)
}

```

When and why might it be a better fit to structure data as an enum with indirection instead of a class, and vice-versa?

---

<div class="post-metadata">

**Author:** ![cukr](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/cukr/32/889_2.png) [@cukr](https://forums.swift.org/u/cukr)\
**Post date:** [April 17, 2022, 4:53pm UTC](https://forums.swift.org/t/what-are-some-examples-where-enumerations-with-indirection-are-preferred-to-a-class/56784/2 "2022-04-17T16:53:59Z")

</div>

> **[Compiler Explorer](https://godbolt.org/z/bnK73cqh4)**
>
> Compiler Explorer is an interactive online compiler which shows the assembly output of compiled C++, Rust, Go (and many more) code.

1. Classes are reference types, enums are value types. They behave very differently when you copy and then modify them.

2. You cannot use just a class, if you want to have recursion you need an enum + a class. The Tree example was so similar to `Optional` that I used it (Optional is an enum with two cases, one empty and one with stuff, similar to the Tree) but there are more complex situations where you would have to define two types instead of one

---

<div class="post-metadata">

**Author:** ![andreas66](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/andreas66/32/19882_2.png) [@andreas66](https://forums.swift.org/u/andreas66)\
**Post date:** [April 17, 2022, 5:55pm UTC](https://forums.swift.org/t/what-are-some-examples-where-enumerations-with-indirection-are-preferred-to-a-class/56784/3 "2022-04-17T17:55:35Z")

</div>

I did a quick & small project to compare:

> **[BooksAndWords/BSTree/Sources/BSTree at main · anttijuu/BooksAndWords](https://github.com/anttijuu/BooksAndWords/tree/main/BSTree/Sources/BSTree)**
>
> main/BSTree/Sources/BSTree

Found out that (with large data sets) enum is significantly slower due to being value type, compared to classes.

Please let me know if you find places for improvements.
