Skip to content

Instantly share code, notes, and snippets.

@BasThomas
Last active November 24, 2017 14:54
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save BasThomas/d9f535ac48e72c215048a95c712ded07 to your computer and use it in GitHub Desktop.
Switching on optional types
let o1nil: Int? = nil
let o1: Int? = 1
func switcher(_ a: Int?, _ b: Int?) {
switch (a, b) {
case (nil, nil):
print("nothing")
case (let thing?, nil):
print("lhs", thing)
case (nil, let thing?):
print("rhs", thing)
case (let left?, let right?):
print(left, right)
}
}
switcher(o1nil, o1nil) // nothing
switcher(o1, o1nil) // lhs 1
switcher(o1nil, o1) // rhs 1
switcher(o1, o1) // 1 1
@BasThomas
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment