Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using 'Error' as a concrete type conforming to protocol 'Error' is not supported #199

Closed
ratkins opened this issue Nov 18, 2016 · 4 comments
Labels

Comments

@ratkins
Copy link
Contributor

ratkins commented Nov 18, 2016

I'm attempting to write a protocol like this:

protocol Response {}
protocol Resource {
    associatedtype ResponseType: Response
    var baseURL: URL { get }
    var path: String { get }
    var method: HTTPMethod { get }
    var headers: [String: String]? { get }
    var parameters: [String: Any]? { get }
    func parse(response: Data) -> Result<ResponseType, Error>
}

But I get the error Using 'Error' as a concrete type conforming to protocol 'Error' is not supported on the final line. I understand this is because of the way Result is declared as Result<T, Error : Error> ..., thus forcing the caller to provide a concrete Error subtype.

The problem I run into is that in my parse() method I would like to just throw up any errors produced by my JSON parsing library (or whatever.) But they're declared as Swift.Error, which means I can't do that.

I understand from reading other issues that if Result was declared Result<T> (and just implied Swift.Error as its error type) it'd degenerate to just an Either and the maintainers don't want that.

But is there a neat way I can get around this without having to explicitly wrap every Error subtype my parse() might throw?

@ikesyo
Copy link
Member

ikesyo commented Nov 18, 2016

#198 would be the help.

@ratkins
Copy link
Contributor Author

ratkins commented Dec 19, 2016

So, in practice, the answer to the question in my last sentence is "No, just declare parse to return func parse(response: Data) -> Result<ResponseType, AnyError>", which works fine in practice.

@ratkins ratkins closed this as completed Dec 19, 2016
@revolter
Copy link

revolter commented May 8, 2017

I don't understand how does #198 help with this.

@ratkins
Copy link
Contributor Author

ratkins commented May 17, 2017

As I said above, I needed to change the return type from Result<..., Error> to Result<..., AnyError>.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants