Created
July 16, 2016 06:01
-
-
Save zats/e3326c3e31e80d036242d7309ddd07dc to your computer and use it in GitHub Desktop.
Optional escape operator
This file contains hidden or 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
// flow controll using do-catch | |
do { | |
try commentBody(¿commentRangeBeforeOffset(¿getNameOffset(dictionary))) | |
} catch let e as OptionalError { | |
} catch { | |
} | |
// folding into optional value. Currently produces double optional | |
let maybeString = try? commentBody(¿commentRangeBeforeOffset(¿getNameOffset(dictionary))) // String?? |
This file contains hidden or 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
enum OptionalError: ErrorProtocol { | |
case UnexpectedOptionalValue | |
} | |
prefix operator ¿ { | |
} | |
prefix func ¿<T>(_ value: T?) throws -> T { | |
if let value = value { | |
return value | |
} | |
throw OptionalError.UnexpectedOptionalValue | |
} | |
// version of ¿ operator to deal with double optionals | |
prefix func ¿<T>(_ value: T??) throws -> T { | |
if let value = value { | |
return try ¿value | |
} | |
throw OptionalError.UnexpectedOptionalValue | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment