I have a Client protocol that looks like this:
protocol Client {
func get<T>(_ url: String) -> Promise<T>
}
Now, the problems start when I try to implement it. I want to have 2 types of clients: AlamofireClient and MockClient (maybe in the future more, like ErrorClient etc. but let's start simple)
so I need something like:
final class AlamofireClient: Client {
func get<T: Decodable>(_ url: String) -> Promise<T> {
(...)
}
}
and:
final class MockClient: Client {
func get<T: Mockable>(_ url: String) -> Promise<T> {
return Promise { seal in
seal.fulfill(T.mock())
}
}
}
here the simple Mockable interface that every entity in the app will implement:
public protocol Mockable {
static func mock() -> Self
}
But I always get the error:
Type 'MockClient' does not conform to protocol 'Client'
Type 'AlamofireClient' does not conform to protocol 'Client'
That's because is not the same as <T: Decodable> and <T: Mockable>
Is there an elegant way to solve this issue? I'm not able to find an elegant solution for this problem. Maybe the whole idea is just bad? I also tried solving the problem with PATs but also no luck there.
func get<T>(_ url: String) -> Promise<T>and that is what a class must declare in order to conform. You cannot say some other thing.