Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Add generic function implementation to protocol based on another generic function declaration

I’d like to create default implementation for convenience protocol function that just calls another protocol function, but can’t figure out what’s wrong with the code:

protocol RequestManagerProtocol {
    func perform<T: Decodable>(_ request: RequestProtocol) async throws -> (T, Int)
    func perform<T: Decodable>(_ request: RequestProtocol) async throws -> T // Convenience function
}

extension RequestManagerProtocol {
    func perform<T: Decodable>(_ request: RequestProtocol) async throws -> T {
        let (obj, _) = perform<T>(request) // Error: Cannot specialize a non-generic definition
        return obj
    }
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

First of all you have to try await the perform call.

And you have to annotate the (return) type rather than specifying the type in angle brackets

extension RequestManagerProtocol {
    func perform<T: Decodable>(_ request: RequestProtocol) async throws -> T {
        let (obj, _) : (T, Int) = try await perform(request)
        return obj
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading