Created
June 18, 2014 18:44
-
-
Save n8gray/d40785bf2fb7af1fb7d0 to your computer and use it in GitHub Desktop.
This is why Optionals should not be allowed in if statement tests
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
val shouldLaunchMissiles : Bool? = false | |
// A million lines later, programmer forgets that shouldLaunchMissiles is an optional | |
if shouldLaunchMissiles { | |
println("The only way to win is not to play") | |
missiles.launch() | |
} else { | |
println("Let there be peace on Earth") | |
kittens.release() | |
} | |
// Confusingly, if you add a "no-op" comparison it works (optionals implicitly unwrap in this case) | |
if shouldLaunchMissiles == true { | |
println("The only way to win is not to play") | |
missiles.launch() | |
} else { | |
println("Let there be peace on Earth") | |
kittens.release() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment