1

I'm trying to create a generic results enum in swift, this is what I have so far:

enum Result<T: Codable>: Error {
    //Indicates that it was a success, and data is the returned result
    case Success(data: T)

    //Indicates that there was an unrecognizable error
    case Failure(error: Error)

    //Some cases for specific status codes
    case Forbidden              //Status code: 403
    case NotAcceptable          //Status code: 406
    case Conflict               //Status code: 409
    case InternalServerError    //Status code: 500
}

And then I try to create an Observable out of it, like this: (The T is specified in the function's call, this is shortened for brevity)

Observable<Result<T>>.create { observer in 
    //Some code to go make an Http requests and return response....
    switch response.result {
        case .success(let value):
            //This works fine
            observer.onNext(Result.success(value))
            observer.onCompleted()
        case .failure(let error):
            //This is where things go wrong.
            observer.onError(Result.failure(error))
    }
}

The problem is when I try to return in the .failure case, it always says Argument type 'Result<_>' does not conform to expected type 'Error' even though Result is an Error type

Am I doing something wrong?

2 Answers 2

1

Other than the fact that your capitalization is all over the place... When you do Result.failure(anError) the compiler can't infer the type of T. To fix just do: Result<T>.failure(anError)

... Or should it be Result<T>.Failure(error: anError)? You have it capitalized and use a named variable in the definition but you use lower-case and don't use a named variable where it's being used. :-(

Sign up to request clarification or add additional context in comments.

1 Comment

I've actually gone ahead and removed the Result enum all together. I know return the value onNext() if it's a success, and an Error enum with all the specific cases in onError(). And I've fixed the capitalization issue, don't know how this one escaped under my nose...
1

What about this?

enum Result<T: Codable, E: Error> {
    case Success(data: T)
    case Failure(error: E)
    // ...
}

I also removed the inheritance because it does not make sense when you actually wrap it by an enum case.

You might also want to put your networking code somewhere inside of Observable.create closure because the response is not available in a sequential manner. The closure instead is escaping the sequential flow like the network request/response does.

2 Comments

And is that handled from the observable part?
My Result modification might fix your Error conformance issue. I extended my answer to explain more about the Observable part.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.