I have an array of a generic Struct, the generic type of the struct extends of the class ResponseBase
. In the function I try to append to the responseMapping
array but no luck there, it always has the following error: Error: Cannot convert value of type 'ResponseMap<ResponseType>' to expected argument type 'ResponseMap<ResponseBase>'
. I already tried to add the Any, AnyClass, AnyObject or whatever Any as the Type but it also didn't really work out.
I tried to do it with Type Erasure but I got confused and I gave up. Anyone knows how to do this correctly?
Code
struct ResponseMap<ResponseType: ResponseBase> {
var fulfill: (ResponseType) -> ()
}
public class MessengerTest {
var responseMapping = [ResponseMap]()
func send<ResponseType, Type>(_ message: Type) -> Promise<ResponseType> where ResponseType: ResponseBase, Type: Base<ResponseType> {
return Promise<ResponseType> { resolver in
let map = ResponseMap(fulfill: resolver.fulfill)
self.responseMapping.append(map) <---- Error
}
}
}
Update This is code which throws the same Error, but without PromiseKit.
class Promise<U> {
let fulfill: (U)->Void
init() {
fulfill = {U in
print("fulfill")
}
}
}
class Base {
}
class Response: Base {
}
class Response1: Base {
}
struct ResponseMap<U: Base> {
let fulfill: (U)->Void
}
var responseMapping = [ResponseMap]()
let promise1 = Promise<Response>()
let promise2 = Promise<Response1>()
let map1 = ResponseMap(fulfill: promise1.fulfill)
let map2 = ResponseMap(fulfill: promise2.fulfill)
responseMapping.append(map1)
responseMapping.append(map2)