ishkawa/APIKit

Trailing where clause for extension of non-generic type

Closed this issue · 3 comments

I produced this error both all of the default JSON Mapping libraries (Himotaki, Argo, Unbox) which refers in this link
trailing where clause for extension of non-generic type 'LoginRequest'

extension LoginRequest where Response: Marshaling {
    func response(from object: Any, urlResponse: HTTPURLResponse) throws -> Login {

    }
}

Hi, @ttuygun.

I guess your LoginRequest is a struct or a class. If you want to provide implementation of response(from:urlResponse:) for a single request type, just remove the where clause or move the method into type declaration.

The guide you mentioned assumes type to extend is a protocol. It demonstrates how to provide implementation of response(from:urlResponse:) for multiple request types which conforms to the common request protocol (GitHubRequest in the example).

To follow this way, make a common request protocol and let LoginRequest to conform to the protocol like this:

protocol YourServiceRequest: Request {
    ...
}

extension YourServiceRequest where Response: Marshaling {
    func response(from object: Any, urlResponse: HTTPURLResponse) throws -> Login {
        fatalError("To be implemented.")
    }
}

struct LoginRequest: YourServiceRequest {
    ...
}

Wow, thank you for information. It really helps to understand about protocols and solving problem.

🎉