Skip to content

Instantly share code, notes, and snippets.

@seadowg
Last active March 10, 2017 09:25
Show Gist options
  • Save seadowg/09725ed0a768b9d787e6c6c73ec81656 to your computer and use it in GitHub Desktop.
Save seadowg/09725ed0a768b9d787e6c6c73ec81656 to your computer and use it in GitHub Desktop.
ADT examples in Swift
enum Direction {
case Left
case Right
}
func angle(direction: Direction): Int {
switch(direction) {
case Left: return 90
case Right: return -90
}
}
enum Result<T> {
case Valid(T)
case Error(Error)
}
// Because switch statements are not expressions it's best to write this example in
// it's own function
func getAddresses(): [Address] {
var addresses = [Address]()
let result = addressesClient.fetchAddresses()
switch (result) {
case .Valid(let value): return value
case .Error(let value): return addresses
}
}
let addresses = getAddresses()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment