Created
August 23, 2021 05:00
-
-
Save JarvisTheAvenger/a231ad54907ef0903cc15989203ea91b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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