Skip to content

Instantly share code, notes, and snippets.

@rnapier
Last active August 29, 2015 14:06
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rnapier/42ef28d52ac92744730f to your computer and use it in GitHub Desktop.
Save rnapier/42ef28d52ac92744730f to your computer and use it in GitHub Desktop.
The ever-important Result type (without methods)
enum Result<A> {
case Success(Box<A>)
case Failure(NSError)
}
// Due to current swift limitations, we have to include this Box in Result.
// Swift cannot handle an enum with multiple associated data (A, NSError) where one is of unknown size (A)
final class Box<T> {
let unbox: T
init(_ value: T) { self.unbox = value }
}
extension Result: Printable {
var description: String {
switch self {
case Success(let box): return "Success: \(box.unbox)"
case Failure(let err): return "Failure: \(err.description)"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment