Last active
March 10, 2017 09:25
-
-
Save seadowg/09725ed0a768b9d787e6c6c73ec81656 to your computer and use it in GitHub Desktop.
ADT examples in Swift
This file contains hidden or 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
enum Direction { | |
case Left | |
case Right | |
} | |
func angle(direction: Direction): Int { | |
switch(direction) { | |
case Left: return 90 | |
case Right: return -90 | |
} | |
} |
This file contains hidden or 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
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