<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">
<div class="">Hi, </div>
<div class=""><br class="">
</div>
I raised this issue a few months back and discussion has died out since.
<div class="">I’m raising this again to see if there are any objections before I submit a proposal.</div>
<div class=""><br class="">
</div>
<div class=""><br class="">
</div>
<div class=""><br class="">
</div>
<div class="">
<div class="">I have a class that conforms to a protocol which declares a method with a specific return type.</div>
<div class="">In case I want to return a subclass of the return type I am forced to use anassociatedtype. </div>
<div class="">This feels like a hack.</div>
<div class=""><br class="">
</div>
<div class=""><b class="">Example:</b></div>
<div class=""><br class="">
</div>
<div class="">// The protocol</div>
<div class="">protocol MyShapeProtocol {</div>
<div class=""><span class="Apple-tab-span" style="white-space:pre"></span>// returns Shape</div>
<div class=""><span class="Apple-tab-span" style="white-space:pre"></span>func make() -> Shape?</div>
<div class="">}</div>
<div class=""><br class="">
</div>
<div class="">// Circle inherits from Shape</div>
<div class="">class Circle : Shape {}</div>
<div class=""><br class="">
</div>
<div class="">// CircleMaker conforms to the MyShapeProtocol</div>
<div class="">class CircleMaker : MyShapeProtocol {</div>
<div class=""><span class="Apple-tab-span" style="white-space:pre"></span>// CircleMaker wants to return Circle which is a type of Shape</div>
<div class=""><span class="Apple-tab-span" style="white-space:pre"></span>func make() ->Circle? {</div>
<div class=""><span class="Apple-tab-span" style="white-space:pre"></span>return Circle()</div>
<div class=""><span class="Apple-tab-span" style="white-space:pre"></span>}</div>
<div class="">}</div>
<div class=""><br class="">
</div>
<div class="">This will not work.</div>
<div class="">For that to work I’ll need to use toe associatedtype “hack”:</div>
<div class=""><br class="">
</div>
<div class=""><b class="">Example:</b></div>
<div class="">protocol MyShapeProtocol {</div>
<div class=""><span class="Apple-tab-span" style="white-space:pre"></span>associatedtype ShapeReturnType : Shape</div>
<div class=""><span class="Apple-tab-span" style="white-space:pre"></span>func make() -> ShapeReturnType?</div>
<div class="">}</div>
<div class=""><br class="">
</div>
<div class="">class Circle : Shape {}</div>
<div class=""><br class="">
</div>
<div class="">class CircleMaker : MyShapeProtocol{</div>
<div class=""><span class="Apple-tab-span" style="white-space:pre"></span>func make() ->Circle? {</div>
<div class=""><span class="Apple-tab-span" style="white-space:pre"></span>return Circle()</div>
<div class=""><span class="Apple-tab-span" style="white-space:pre"></span>}</div>
<div class="">}</div>
</div>
<div class=""><br class="">
</div>
<div class="">What I’m suggesting is to allow to return a subclass for a protocol method without the need for an associatedtype.</div>
<div class=""><br class="">
</div>
<div class="">Any reason why not?</div>
</body>
</html>