Skip to content

Instantly share code, notes, and snippets.

@loganmoseley
Last active October 10, 2017 16:59
Show Gist options
  • Save loganmoseley/5fa1f8777b1cf8caee2a785ffccee4b2 to your computer and use it in GitHub Desktop.
Save loganmoseley/5fa1f8777b1cf8caee2a785ffccee4b2 to your computer and use it in GitHub Desktop.
import Foundation
import Result
public extension Result {
/// Constructs a success wrapping `value`, iff `value` is not nil and `error` is nil.
///
/// Constructs a failure wrapping `error`, iff `error` is not nil and `value` is nil.
///
/// Otherwise, returns nil.
///
/// Example:
///
/// request(url: examplecom) { (maybeJSON, maybeError) in
/// guard let result = Result(value: maybeJSON, error: maybeError) else {
/// fatalError("invalid callback parameters")
/// }
///
/// // result : Result<String, NSError>
/// print(result)
/// }
public init?(value v: T?, error e: Error?) {
switch (v, e) {
case let (.some(v), .none):
self = .success(v)
case let (.none, .some(e)):
self = .failure(e)
case (_, _):
return nil
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment