Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save BrandonShega/a99cae77971c217bd1d6fd381416e85d to your computer and use it in GitHub Desktop.
Save BrandonShega/a99cae77971c217bd1d6fd381416e85d 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")
/// }
///
/// // decoded : Result<String, NSError>
/// print(decoded)
/// }
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