Last active
August 29, 2015 14:06
-
-
Save rnapier/42ef28d52ac92744730f to your computer and use it in GitHub Desktop.
The ever-important Result type (without methods)
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 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