Skip to content

Instantly share code, notes, and snippets.

@JarvisTheAvenger
Created August 23, 2021 05:00
Show Gist options
  • Save JarvisTheAvenger/a231ad54907ef0903cc15989203ea91b to your computer and use it in GitHub Desktop.
Save JarvisTheAvenger/a231ad54907ef0903cc15989203ea91b to your computer and use it in GitHub Desktop.
import Foundation
// Indirect enums are enums that need to reference themselves somehow,
// and are called “indirect” because they modify the way Swift stores them so they can grow to any size.
// Without the indirection, any enum that referenced itself could potentially become infinitely sized:
// it could contain itself again and again, which wouldn’t be possible
indirect enum LinkedListItem<T> {
case endNode(value: T)
case nextNode(value: T, next: LinkedListItem)
}
let third = LinkedListItem.endNode(value: "third")
let second = LinkedListItem.nextNode(value: "second", next: third)
let first = LinkedListItem.nextNode(value: "first", next: second)
var currentNode = first
whileLoop : while true {
switch currentNode {
case .endNode(value: let value):
print(value)
break whileLoop
case .nextNode(value: let value, next: let nextItem):
print(value)
currentNode = nextItem
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment