<div dir="ltr">Hi all,<div><br></div><div>Covariance works properly with classes:</div><div>
<p class=""><span class="">class</span><span class=""> M</span>yType {}<br>class MySubtype: MyType {}<br></p><div>class MyClass {</div><div> func createMyType() -> MyType {</div><div> return MyType()</div><div> }</div><div>}</div><div><br></div><div>class MySubClass: MyClass {</div><div> override func createMyType() -> MySubtype {</div><div> return MySubtype()</div><div> }</div><div>}</div><div><br></div><div>However it doesn't work for protocol conformance:</div><div><br></div><div><div>protocol MyProtocol {</div><div> func createMyType() -> MyType</div><div>}</div><div><br></div><div>class MyConformingClass: MyProtocol {</div><div> func createMyType() -> MySubtype {</div><div> return MySubtype()</div><div> }</div><div>}</div></div><div><br></div><div>Compiler error for this case:</div><div><div>error: type 'MyConformingClass' does not conform to protocol 'MyProtocol'</div><div>note: protocol requires function 'createMyType()' with type '() -> MyType'</div><div>note: candidate has non-matching type '() -> MySubtype'</div></div><div><br></div><div>I know I can fix it by using associated objects in MyProtocol definition, but then if I want to declare a property of type MyProtocol I have to go dancing with generics everywhere.</div><div><br></div><div>Is this a known limitation/bug? Any plans to improve this?</div><div><br></div><div>Cheers,</div><div>Diego</div><p class=""><span class=""></span></p></div></div>