Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This is why Optionals should not be allowed in if statement tests
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
You can’t perform that action at this time.