Created
February 25, 2021 22:51
-
-
Save manas-sharma-1683/422fa1ea015b3f792271ab2a4cc2083b to your computer and use it in GitHub Desktop.
??? operator in swift.
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
infix operator ??? | |
func ???<T> (_ value: @autoclosure () -> T?, _ error: @autoclosure () -> Error) throws -> T { | |
guard let value = value() else { | |
throw error() | |
} | |
return value | |
} | |
do { | |
let value = Optional(20) ??? NSError() | |
print(value) | |
} catch { | |
print(error) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment