Skip to content

Instantly share code, notes, and snippets.

@fchaillou
Created March 1, 2016 00:00
Show Gist options
  • Save fchaillou/c9f798a747b939728aca to your computer and use it in GitHub Desktop.
Save fchaillou/c9f798a747b939728aca to your computer and use it in GitHub Desktop.
Alamofire Result to Antitypical Result conversion
import Alamofire
import Foundation
import enum Result.Result
extension Alamofire.Result {
func toStandard() -> Result<Value, Error> {
switch(self) {
case Alamofire.Result.Success(let value) : return Result.Success(value)
case Alamofire.Result.Failure(let error) : return Result.Failure(error)
}
}
}
extension Response {
func standardResult() -> Result<Value, Error> {
return self.result.toStandard()
}
}
@KingOfBrian
Copy link

This is another approach for Result interoperability. It adds Result.ResultType conformance to Alamofire.Result. Appears to work pretty well.

extension Alamofire.Result: ResultType {

    /// Constructs a success wrapping a `value`.
    public init(value: Value) {
        self = .Success(value)
    }

    /// Constructs a failure wrapping an `error`.
    public init(error: Error) {
        self = .Failure(error)
    }

    /// Case analysis for Result.
    ///
    /// Returns the value produced by applying `ifFailure` to `Failure` Results, or `ifSuccess` to `Success` Results.
    public func analysis<Result>(@noescape ifSuccess ifSuccess: Value -> Result, @noescape ifFailure: Error -> Result) -> Result {
        switch self {
        case let .Success(value):
            return ifSuccess(value)
        case let .Failure(value):
            return ifFailure(value)
        }
    }

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment