[swift-users] Refining generics in classes

Jon Shier jon at jonshier.com
Wed Nov 15 22:42:48 CST 2017


	Swift Users:
	I have a generics use case which has somewhat stumped me. I have two related protocols, JSONDecodable and CompressedDecodable, and CompressedDecodable inherits from JSONDecodable (though that relationship isn’t strictly necessary). I also have a generic function that’s overloaded for each of those protocols. I’m trying to write a class to make a network request expecting a generic response type of either JSONDecodable or CompressedDecodable. However, it doesn’t seem possible to write it in such a way that the overload I need is called. Instead, it’s always the superclass’ type’s overload that is called. For example:

protocol JSONDecodable { init() }
protocol CompressedDecodable: JSONDecodable { }

class NetworkRequest<T: JSONDecodable> {
    
    var response: T?
    
    func doAThing() {
        response = doSomething()
    }
}

class CompressedNetworkRequest<U: CompressedDecodable>: NetworkRequest<U> {
    
}

func doSomething<T: JSONDecodable>() -> T {
    print("One: \(T.self)")
    return T()
}

func doSomething<T: CompressedDecodable>() -> T {
    print("Two: \(T.self)")
    return T()
}

struct Uno: JSONDecodable { }
struct Dos: CompressedDecodable { }

NetworkRequest<Uno>().doAThing()
CompressedNetworkRequest<Dos>().doAThing()

In a playground this prints:

One: Uno
One: Dos

Ultimately, I understand why this happens (NetworkRequest’s generic type is always going to be JSONDecodable, no matter if it’s actually a subtype). Is there any way, aside from completely duplicating the class, to call the overload appropriate for the type passed in a class like this? 



Jon Shier


More information about the swift-users mailing list