<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=""><br class=""><div><blockquote type="cite" class=""><div class="">On May 13, 2016, at 1:11 PM, Nicola Salmoria via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><br class=""><div class="gmail_extra"><br class=""><div class="gmail_quote">On Fri, May 13, 2016 at 12:55 PM, Vladimir.S <span dir="ltr" class="">&lt;<a href="mailto:svabox@gmail.com" target="_blank" class="">svabox@gmail.com</a>&gt;</span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">&gt; I'm not convinced by this example.<br class="">
<br class="">
Probably my &amp; Matthew's previous discussion in `[swift-evolution] [RFC] #Self` topic will help you. I was trying there to find out (in primitive examples) what Matthew is trying to achive with this `-&gt;StaticSelf` in protocol.<br class="">
<br class="">
In two words - he wants to achieve requirement 'return Self class or any of base classes', as current `-&gt;Self` requires 'strictly return Self class'<br class=""></blockquote><div class=""><br class=""></div><div class="">I think I understand what the request is, but I'm not sure if it's a problem worth solving, and if this would be the right solution.<br class=""></div><div class="">&nbsp;<br class=""></div><div class="">Going back to the example, let's say you have the requested<br class=""><br class="">protocol Fooable {<br class="">
&nbsp; &nbsp; func foo() -&gt; StaticSelf<br class="">
}<br class=""><br class="">class A: Fooable {<br class="">&nbsp; &nbsp; func foo() -&gt; A { return A() }<br class="">
}<br class=""></div><div class=""><br class=""></div><div class="">class B: A {<br class="">}<br class=""><br class=""></div><div class="">How would you use foo() in generic code?<br class=""><br class=""></div><div class="">func bar&lt;T: Fooable&gt;(_ x: T) -&gt; X {<br class=""></div><div class="">&nbsp;&nbsp;&nbsp; return x.foo()<br class="">}<br class=""><br class=""></div><div class="">What does bar() return? What do you put in place of X in its declaration?<br class=""></div><div class="">You can't use T, you can't use T.StaticSelf. So what's the purpose of having a protocol if you can't use it in generic code?<br class=""></div></div></div></div></div></blockquote><div><br class=""></div>This is a good question. &nbsp;Thank you very much for providing a concrete example along with the question.</div><div><br class=""></div><div>Let’s look at an example of what some of us have in mind:</div><div><br class=""></div><div>protocol&nbsp;StringCreatable {<div class="">&nbsp; &nbsp; static&nbsp;func&nbsp;createWithString(s: String)&nbsp;-&gt;&nbsp;StaticSelf</div><div class="">}</div><div class=""><br class=""></div><div class="">extension&nbsp;NSURL:&nbsp;StringCreatable {<br class="">&nbsp; &nbsp; static&nbsp;func&nbsp;createWithString(s: String)&nbsp;-&gt;&nbsp;StaticSelf {&nbsp;</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; // ...</div><div class=""><div class="">&nbsp; &nbsp; }</div><div class="">}</div></div><div class=""><br class=""></div></div><div>func foo&lt;Result: StringCreatable&gt;(s: String) -&gt; Result {</div><div>&nbsp; &nbsp; return Result.createWithString(s: s)</div><div>}</div><div><br class=""></div><div>Obviously this will not work properly because we are not guaranteed that `createWithString` returns Result (that is only possible if the return type is Self). &nbsp;We would have to do the following:</div><div><br class=""></div><div>protocol&nbsp;StringCreatable {</div><div>&nbsp; &nbsp; typealias ConformingType = StaticSelf<br class=""><div class="">&nbsp; &nbsp; static&nbsp;func&nbsp;createWithString(s: String)&nbsp;-&gt;&nbsp;StaticSelf</div><div class="">}</div><div class=""><br class=""></div><div class=""><div>func foo&lt;Result: StringCreatable where Result.ConformingType == Result&gt;(s: String) -&gt; Result {</div><div>&nbsp; &nbsp; return Result.createWithString(s: s)</div><div>}</div></div><div><br class=""></div><div>This requires same type constraints. &nbsp;I believe that is coming as part of “completing generics”. &nbsp;</div><div><br class=""></div><div>However, it also raises a question: if the conformance afforded to subclasses can’t actually be used in a useful manner they probably shouldn’t have that conformance in the first place. &nbsp;If the conformance isn’t inherited then we don’t need StaticSelf at all (we can just use Self and still conform the visible class of a class cluster). &nbsp;This is the point Joe has been making all along. &nbsp;Working through the example has helped me understand this point of view better.</div><div><br class=""></div><div>I wonder if anyone has any other examples where subclass conformance would actually be useful. &nbsp;If so, please share. &nbsp;Those who are supporting this proposal: how do you envision using StaticSelf in your code? &nbsp;What are some examples where you have had trouble due to the current limitations of the language?</div><div><br class=""></div><div class="">-Matthew</div></div><div><br class=""></div><div><br class=""></div><div><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class="gmail_extra"><div class="gmail_quote"><div class=""><br class=""></div><br class=""></div><div class="gmail_quote">Nicola<br class=""><br class=""></div></div></div>
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></body></html>