1 year ago

#327948

test-img

Anton Unt

Function as a variable with generics in it?

I am trying to create a variable of a generic function type:

import Foundation

typealias Processor<T:Decodable> = (T.Type, URL) -> Promise<T>

struct Service {
    let processor: Processor

    func getAPI1() -> Promise<TypedResponse1> {
        let url = ...
        return processor(TypedResponse1.self, url)
    }
    
    func getAPI2(params: [String]) -> Promise<TypedResponse2> {
        let url = ...
        return processor(TypedResponse2.self, url)
    }

}

Compiling gives me an error:

Reference to generic type 'Processor' requires arguments in <...>

However, I don't want to specialize the variable since it kinda beats the purpose of having it and will be more difficult to test.

How can I accomplish this?

ios

swift

generics

functional-programming

foundation

0 Answers

Your Answer

Accepted video resources