Switching on optional types
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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I wrote a blogpost on this: https://basthomas.github.io/switches-and-cases