1

I am having a generic class, let's say DataProvider<T,S> Now I want to retrieve all different DataProviders from a method,like getProviders(). However, due to Swift having compile time generics, this doesn't seem to be possible?

func getProvider() -> [DataProvider]
{
     let prov1 = DataProvider<Foo,Bar>()
     let prov2 = DataProvider<Foo2,Bar2>()
     return [prov1,prov2]
}

All the methods of this DataProvider also use generic parameters/return values, so I can't let it confirm to a non-generic protocol. Is this just the limitation of Swift, or are there clever ways to get around this?

Peterdk
  • 15,625
  • 20
  • 101
  • 140
  • 3
    Possible duplicate of [How do I store a value of type Class in a Dictionary of type \[String:Class\] in Swift?](https://stackoverflow.com/questions/38590548/how-do-i-store-a-value-of-type-classclassimplementingprotocol-in-a-dictionary) – Dávid Pásztor Oct 12 '18 at 09:33
  • Also related [Swift generic coercion misunderstanding](https://stackoverflow.com/questions/41976844/swift-generic-coercion-misunderstanding) and [Why can't Swift automatically convert a generic type parameter to its superclass](https://stackoverflow.com/questions/50859027/why-cant-swift-automatically-convert-a-generic-type-parameter-to-its-superclass) – Dávid Pásztor Oct 12 '18 at 09:34

0 Answers0