<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 <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> 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=""><<a href="mailto:svabox@gmail.com" target="_blank" class="">svabox@gmail.com</a>></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">> I'm not convinced by this example.<br class="">
<br class="">
Probably my & 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 `->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 `->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=""> <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="">
func foo() -> StaticSelf<br class="">
}<br class=""><br class="">class A: Fooable {<br class=""> func foo() -> 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<T: Fooable>(_ x: T) -> X {<br class=""></div><div class=""> 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. 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 StringCreatable {<div class=""> static func createWithString(s: String) -> StaticSelf</div><div class="">}</div><div class=""><br class=""></div><div class="">extension NSURL: StringCreatable {<br class=""> static func createWithString(s: String) -> StaticSelf { </div><div class=""> // ...</div><div class=""><div class=""> }</div><div class="">}</div></div><div class=""><br class=""></div></div><div>func foo<Result: StringCreatable>(s: String) -> Result {</div><div> 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). We would have to do the following:</div><div><br class=""></div><div>protocol StringCreatable {</div><div> typealias ConformingType = StaticSelf<br class=""><div class=""> static func createWithString(s: String) -> StaticSelf</div><div class="">}</div><div class=""><br class=""></div><div class=""><div>func foo<Result: StringCreatable where Result.ConformingType == Result>(s: String) -> Result {</div><div> return Result.createWithString(s: s)</div><div>}</div></div><div><br class=""></div><div>This requires same type constraints. I believe that is coming as part of “completing generics”. </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. 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). This is the point Joe has been making all along. 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. If so, please share. Those who are supporting this proposal: how do you envision using StaticSelf in your code? 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>