Created
February 24, 2016 03:50
-
-
Save MichaelShaw/ef9f1bae81d43df0a1b8 to your computer and use it in GitHub Desktop.
Reliable Swift Segfault
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
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