Last active
August 29, 2015 14:02
-
-
Save Grantismo/3e1ba0412e911dfd7cfe to your computer and use it in GitHub Desktop.
Optional chaining for arbitrary operations
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
extension Optional{ | |
func omap<K:Any>(optionalBlock:(T)->(K?))->K?{ | |
if self{ | |
return optionalBlock(self!) | |
} | |
return nil | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Swift introduces optional chaining which allows for succinct error handling:
It's difficult however to chain other operators with optionals succinctly
omap (optional map) allows succinct optional chaining with other operators. If the optional has a value, the value is passed to the user supplied closure. Otherwise a nil optional is returned.
or