Last active
July 19, 2016 18:21
-
-
Save SimplGy/acc81523ceeef1f364d5148fa8e5d68e to your computer and use it in GitHub Desktop.
Switching on optional enums
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
enum Coin { | |
case heads | |
case tails | |
} | |
var result: Coin? | |
// You can pattern match against it like an optional: | |
switch result { | |
case .heads?: print("heads") | |
case .tails?: print("tails") | |
case nil: print("not yet flipped") // exhaustive | |
} | |
// Or, you can pattern match it agains the the optional enum that wraps all optional types: | |
switch result { | |
case .Some(.heads): print("heads") | |
case .Some(.tails): print("tails") | |
case .None: print("not yet flipped") // also exhaustive | |
} | |
// This even works on optional booleans | |
switch tab?.paid { | |
case true?: | |
tabState = .paid | |
case false?: | |
tabState = .open | |
case nil: | |
tabState = .empty | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment