How to reference a generic class with protocol restriction with titles in Swift?

I am trying to define a P2 protocol so that it returns a generic class with a restriction on another P1 protocol, for example:

 protocol P1 {} class C<T : P1> {} public protocol P2 { typealias T class func c() -> C<T> } 

But this leads to the following compiler error:

 error: type 'T' does not conform to protocol 'P1' class func c() -> C<T> 

It seems that there is no combination that allows this, for example. following obvious syntax:

 protocol P1 {} class C<T : P1> {} public protocol P2 { typealias T class func c() -> C<T : P1> } 

Errors with:

 error: expected '>' to complete generic argument list class func c() -> C<T : P1> ^ note: to match this opening '<' class func c() -> C<T : P1> 

Can this be done in Swift?

+6
source share
1 answer

I have never used such restrictions, but I think you can simply define it in typealias - I tried it on the playground, and the compilation was successful:

 typealias T: P1 
+6
source

Source: https://habr.com/ru/post/981542/


All Articles