Fast protocol inheritance

I have a quick code:

import Foundation protocol ParentProtocol { //stuff } protocol ChildProtocol:ParentProtocol { //additional stuff } protocol FooProtocol { var variable:ParentProtocol? { get } } class Foo:FooProtocol { var variable:ChildProtocol? } 

I have a compiler error: Type 'Foo' does not conform to protocol 'FooProtocol'

I know that according to FooProtocol type of the variable should be ParentProtocol . On the other hand, ChildProtocol inherits from ParentProtocol , so it is also ParentProtocol

Is there any solution for using protocol inheritance in this way?

+6
source share
1 answer

I found a solution with related types ( fooobar.com/questions/1008992 / ... )

In my case it will be:

 protocol FooProtocol { associatedtype T = ParentProtocol var variable:T? { get } } 
+2
source

All Articles