Nested Generics and Recursive Enum

Introduction

Welcome to the first lesson of Advanced Swift. So far, you’ve learned how to create generic structs, classes, enums, and functions. Today, you will learn how to add generics within generics. Next, you will learn the meaning of indirect enums using the binary data structure.

Problem

  1. Learn how to created nested generic enum
  2. Learn more about recursive enum with binary tree

Nested Generics

Let us take your game to the next level with generics.

Before Swift 3.1

To appreciate nested generics, let us look into your/my past.

Design Generic Enums

Create two generic enums: Gadget and Company.

  1. enum Gadget<T> {
  2. case smartphone
  3. case laptop
  4. case fridge
  5. case others(T)
  6. }
  7. enum Company<T> {
  8. case Samsung
  9. case Apple
  10. case Sony
  11. case others(T)
  12. }

Design Struct

Create a struct called, Product. It contains three properties: company, gadget, and reviews.

  1. struct Product<T> {
  2. let company: Company<T>
  3. let gadget: Gadget<T>
  4. let reviews: [T]
  5. init(enterCompany: Company<T>, enterGadget: Gadget<T>, enterReview: [T]) {
  6. company = enterCompany
  7. gadget = enterGadget
  8. reviews = enterReview
  9. }
  10. }

The init method seems unpleasant. Let us attempt to initialize.

  1. let myProduct = Product(enterCompany: .Apple,
  2. enterGadget: .fridge,
  3. enterReview: ["Good, silver, but expensive"])

There must be an alternative.

Swift 3.1

Now, you may add a generic type within a generic type. Create a generic struct, Team. Within the struct, it also contains another generic structure, TeamMember.

  1. struct Team<T> {
  2. struct TeamMember {
  3. let name: T?
  4. let age: T?
  5. }
  6. }

Let us use nested generics using the Productexample.

  1. struct NestedProduct<T> {
  2. indirect enum Gadget {
  3. case smartphone
  4. case laptop
  5. case fridge
  6. case others(T)
  7. }
  8. indirect enum Company {
  9. case Samsung
  10. case Apple
  11. case Sony
  12. case others(T)
  13. }
  14. let company: Company
  15. let gadget: Gadget
  16. let reviews: [T]
  17. init(enterCompany: Company, enterGadget: Gadget, enterReView: [T]) {
  18. company = enterCompany
  19. gadget = enterGadget
  20. reviews = enterReView
  21. }
  22. }

Important: The indirect keyword is needed in front of Company and Gadget due to a bug. You will learn more about the meaning of indirect soon.

Cyclic Dependency: A relation between two or more modules which either directly or indirectly depend on each other to function properly. The tight coupling of the mutually dependent modules which reduces or makes impossible the separate re-use of a single module.

Let us initialize using the generic enums

  1. let myFridge = NestedProduct(enterCompany: .Apple,
  2. enterGadget: .laptop,
  3. enterReView: ["Good"])

You are following the principle: write less, produce more.

Recursive Enum

Before I introduce you to the meaning of indirect, let us take a look the characteristic of enum associated value.

First, let us design blueprints using class, struct, and enum.

  1. class BobClass {}
  2. struct BobStruct {}
  3. enum BobEnum {
  4. case bobCase
  5. }

Let us initialize

  1. let bobClass = BobClass()
  2. let bobStruct = BobStruct()
  3. let bobEnum = BobEnum.bobCase

Create an enum, MyEnum, with associated value.

  1. enum MyEnum {
  2. case myClass(BobClass)
  3. case mySruct(BobStruct)
  4. case myEnum(BobEnum)
  5. }

Let us initialize.

  1. MyEnum.myClass(bobClass)
  2. MyEnum.mySruct(bobStruct)
  3. MyEnum.myEnum(bobEnum)

Let us visualize.

Nested Generics with Recursive Enum - 图1

Notes: The associated value of myStruct and myEnum are stored while myClass is referenced somewhere in the cloud.

The Meaning of indirect enum with Binary Tree

Let us create an enum called, Tree. There are two cases, either Empty or Node. The Node case has associated value.

  1. indirect enum Tree {
  2. case Empty
  3. case Node(value: Int ,left: Tree ,right: Tree)
  4. }

Let us create a couple nodes.

  1. let tree1 = Tree.Node(value: 1, left: Tree.Empty, right: Tree.Empty)
  2. let tree2 = Tree.Node(value: 2, left: Tree.Empty, right: Tree.Empty)
  3. let tree3 = Tree.Node(value: 3, left: tree1, right: tree2)

Let us visualize

Nested Generics with Recursive Enum - 图2

Let us add two more nodes

  1. let tree1 = Tree.Node(value: 1, left: Tree.Empty, right: Tree.Empty)
  2. let tree2 = Tree.Node(value: 2, left: Tree.Empty, right: Tree.Empty)
  3. let tree3 = Tree.Node(value: 3, left: tree1, right: tree2)
  4. let tree4 = Tree.Node(value: 4, left: tree3, right: tree5)
  5. let tree5 = Tree.Node(value: 5, left: Tree.Empty, right: Tree.Empty)

Let us visualize

Nested Generics with Recursive Enum - 图3

Important: indirect enums no longer store associated value. It references. For instance, the associated value of tree4 has a reference to tree5 and tree3. tree3 has a reference to tree1 and tree2.

Recursive Function to Sum Values

Let us calculate the sum of every tree from the top using a recursive enum.

  1. func evaluateTree(tree:Tree) -> Int {
  2. switch tree {
  3. case .Empty:
  4. return 0
  5. case .Node(let value, let left, let right):
  6. return value + evaluateTree(tree: left) + evaluateTree(tree: right)
  7. }
  8. }

Let us test.

  1. evaluateTree(tree: tree1) // 1
  2. evaluateTree(tree: tree3) // 6

Important: You may have to dissect the function multiple times to understand. I recommend you to start using a smaller node.

Source Code

8001_nested_generics_recursive_enum

References

Generic struct crashes because of “cyclic metadata dependency

My Question on Stack Overflow

Conclusion

There were two objectives. First, you’ve learned how to design beautiful code with nested generics. Second, you’ve learned the difference between normal enums and indirect enums when it comes to associated value. Remember, indirect enums point rather than store. Some may be baffled by the recursive function to add the value of all nodes. Well, I can’t help you much. I recommend you to start with one node and move up.

In the following lesson, finally, you will learn how to create custom operators.

Note: Learn Swift with Bob is available on Udemy. If you wish to receive a discount link, you may sign up here.