Skip to content

Instantly share code, notes, and snippets.

@cmsj
Last active October 22, 2025 11:56
Show Gist options
  • Save cmsj/4767f94c91e3f0c45bd4f0b88c9fcfb7 to your computer and use it in GitHub Desktop.
Save cmsj/4767f94c91e3f0c45bd4f0b88c9fcfb7 to your computer and use it in GitHub Desktop.
func mightFail() -> Bool? { // Return type of this is actually Optional(Bool) because of the ?
return nil
}
func main() {
guard let result = mightFail() else { // This unwraps the Optional(Bool)
print("lol get rekt")
return
}
print("Result was: \(result == true)") // No compiler error about `result`, it is not possible for it to be nil here
}
@cmsj
Copy link
Author

cmsj commented Oct 22, 2025

Or if you hate yourself:

func mightFail() -> Bool? {
  return nil
}

func main() {
  let result = mightFail()
  print("Result was \(result! == true)") // Optional(Bool) `result` is force-unwrapped with the !. Runtime crash if it's nil.
}

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