Skip to content

Instantly share code, notes, and snippets.

@nickmain
Created June 14, 2014 22:36
Show Gist options
  • Save nickmain/9a7a28f77c8c780876aa to your computer and use it in GitHub Desktop.
Save nickmain/9a7a28f77c8c780876aa to your computer and use it in GitHub Desktop.
Using pattern matching on tuples of Optionals
// Playground - noun: a place where people can play
import Cocoa
func checkOptionals(a:Dictionary<String,Int>,key1:String,key2:String) {
switch (a[key1],a[key2]) {
case (.Some(_),.Some(_)): println("both")
case (.Some(let a),.None): println("first " + String(a))
case (.None,.Some(let b)): println("second " + String(b))
default: println("neither")
}
}
let dict = ["x":5,"y":6,"z":7]
checkOptionals(dict,"g","h")
checkOptionals(dict,"x","g")
checkOptionals(dict,"h","y")
checkOptionals(dict,"x","z")
@nickmain
Copy link
Author

Console output is:

    neither
    first 5
    second 6
    both

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