Skip to content

Instantly share code, notes, and snippets.

@MichaelShaw
Created February 24, 2016 03:50
Show Gist options
  • Save MichaelShaw/ef9f1bae81d43df0a1b8 to your computer and use it in GitHub Desktop.
Save MichaelShaw/ef9f1bae81d43df0a1b8 to your computer and use it in GitHub Desktop.
Reliable Swift Segfault
public enum MyResult<T, Error: ErrorType> {
case Success(T)
case Failure(Error)
}
func doThing(@noescape block: (() -> Void)) throws {
try block()
}
func throwableToMyResult<T>(block: (() throws -> MyResult<T, NSError>)) -> MyResult<T, NSError> {
do {
let result = try block()
return result
} catch let error as NSError {
return MyResult.Failure(error)
}
}
func blowUp<T>(f: () -> MyResult<T, NSError>) -> MyResult<T, NSError> {
return throwableToMyResult { () -> MyResult<T, NSError> in
try doThing {
return f()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment