Skip to content

Instantly share code, notes, and snippets.

@n8gray
Created June 18, 2014 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save n8gray/d40785bf2fb7af1fb7d0 to your computer and use it in GitHub Desktop.
Save n8gray/d40785bf2fb7af1fb7d0 to your computer and use it in GitHub Desktop.
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