Skip to content

Instantly share code, notes, and snippets.

@zats
Created July 16, 2016 06:01
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 zats/e3326c3e31e80d036242d7309ddd07dc to your computer and use it in GitHub Desktop.
Save zats/e3326c3e31e80d036242d7309ddd07dc to your computer and use it in GitHub Desktop.
Optional escape operator
// 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??
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