0

When I want create specialized class from generics I get this error:

error: using 'P2' as a concrete type conforming to protocol 'P1' is not supported

From code:

protocol P1 {
    func foo()
}

protocol P2: P1 {

}

class C1<T: P1> {

}

class C2: C1<P2> {

}

I don't understand why it is not possible. Is known some workaround?

Kryštof Matěj
  • 462
  • 3
  • 16
  • 1
    As Martin said (before the comment was deleted due to the dupe close), `P2` does not conform to `P1`. Depending on your exact situation, you probably want to build a type erased `AnyP2` wrapper, then talk in terms of `C1`. – Hamish May 11 '17 at 20:22
  • @Hamish The lack of language support for existential types strikes again :( – Alexander May 11 '17 at 20:33
  • @Alexander There are some good reasons why this limitation exists, namely static protocol requirements (which I go into detail in [my answer there](http://stackoverflow.com/a/43408193/2976878)) – but yeah, it's a shame the compiler doesn't currently make a distinction between protocols with and without static requirements. – Hamish May 11 '17 at 20:34
  • 1
    @Hamish Oh that's an interesting edge case I didn't foresee. Welcome to type system design, I suppose :p – Alexander May 11 '17 at 22:30

0 Answers0