Forked from loganmoseley/Result+OldCallbackHelp.swift
Created
October 9, 2017 14:53
-
-
Save BrandonShega/a99cae77971c217bd1d6fd381416e85d 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") | |
/// } | |
/// | |
/// // 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