0

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)

Mika
  • 35
  • 1
  • 4
  • When you declare your method `func send` `ResponseType` is just a generic type as well as `Type`. You should use any single letter, usually `T`, in this case to represent your newly created types. They have no relationship the one you used here `struct ResponseMap {`. – Leo Dabus Oct 09 '19 at 12:17
  • @LeoDabus Yes this is true, but it will not change the Error, it will only change the naming of the Types.... – Mika Oct 09 '19 at 12:23
  • I never said it would. Just for you to realize what you are actually doing. `func send(_ message: Type)` is the same as `func send(_ message: U)` – Leo Dabus Oct 09 '19 at 12:24
  • @LeoDabus okay thx, any idea on how to fix the issue? – Mika Oct 09 '19 at 12:37
  • @LeoDabus, I added some code which throws the same error, but without the promisekit. I would really appreciate it if you could have a look :) – Mika Oct 09 '19 at 13:21
  • try `Promise` instead of `Promise` or change your struct declaration to `struct ResponseMap {` – Leo Dabus Oct 09 '19 at 13:40
  • @LeoDabus, okay yes in this case you are right. But in my case, I will have multiple classes extending from Base, so I try to append all classes extending from Base into the array. – Mika Oct 09 '19 at 14:08
  • This is due to generics being invariant in Swift, this is why you cannot append to the array. There are many SO questions on this topic, a good one is https://stackoverflow.com/questions/41976844/swift-generic-coercion-misunderstanding – Cristik Oct 09 '19 at 17:44

0 Answers0