Last active
October 10, 2017 16:59
-
-
Save loganmoseley/5fa1f8777b1cf8caee2a785ffccee4b2 to your computer and use it in GitHub Desktop.
This file contains 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
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