I would like to have a protocol that looks something like this:
protocol ReturnType {
var returnType: ImmutableMappable.Type { get }
}
The part of the enum implementing the protocol:
extension ShimEndPoint: ReturnType {
var returnType: ImmutableMappable.Type {
switch self {
case .deAuthorize(_, _):
return EmptyResponse.self
case .authorize(_, _):
return AuthorizeResponse.self
case .step(_, _, _, _):
return StepResponse.self
}
}
}
EmptyResponse, AuthorizeResponse and StepResponse all implement ImmutableMappable. Now I would like to use the "returnType" property in a function call:
return Shim.provider
.request(endPoint)
.timeout(7,
scheduler: MainScheduler.asyncInstance)
.retry(3)
.mapObject(endPoint.returnType)
The line mapObject gives me the following compiler error: "Cannot convert value of type 'ImmutableMappable.Type' to expected argument type 'T.Type'
The function signature of "mapObject" is:
public func mapObject<T : ImmutableMappable>(_ type: T.Type) -> RxSwift.Observable<T>
How do I define and implement the protocol so I can pass in my returnType to the mapObject function?
I found a similar question but unfortunately I could not solve my problem with the help of the answer given: Returning constrained generics from functions and methods