I have tried to keep my question very simple. I have been struggling whole day to make it work but no luck. I have two protocols. Decodable and Requestable.
protocol Decodable { }
struct D: Decodable { }
protocol Requestable {
associatedtype Model
}
extension Requestable {
func returnValue() {
Utils.doSomething(Model)
}
}
And a class Utils
class Utils {
static func doSomething<T>(param: T) {
print("Default")
}
static func doSomething<T: Decodable>(param: T) {
print("Decodable")
}
static func doSomething<T: Decodable>(param: [T]) {
print("Decodable Array")
}
}
I create a struct R implementing Requestable and give the type alias Model to String
struct R: Requestable {
typealias Model = String
}
When i run the R().returnValue() function, It prints Default. As Expected.
I create a struct R2 implementing Requestable and give the type alias Model to D which is implementing Decodable
struct R2: Requestable {
typealias Model = D
}
When i run the R2().returnValue() function, It prints Default. but i was expecting it would print Decodable because the Model D conforms to Decodable.
I create a struct R3 implementing Requestable and give the type alias Model to [D] where the element of array is implementing Decodable
struct R3: Requestable {
typealias Model = [D]
}
When i run the R3().returnValue() function, It prints Default. but i was expecting it would print Decodable Array because the Model D conforms to Decodable.
Any kind of help appreciated.
UPDATE
Using AnyRequestable and checking in runtime will not work in this case because in real code the Generic is the return value and could not be checked dynamically.
In real code functions signatures are like
public static func ResponseSerializer<M>() -> ResponseSerializer<M, NSError> {}
public static func ResponseSerializer<M: Decodable>() -> ResponseSerializer<M, NSError> {}
public static func ResponseSerializer<M: Decodable>() -> ResponseSerializer<[M], NSError> {}