<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div>Inline<br><br><div>Regards</div>(From<span class="Apple-style-span" style="-webkit-tap-highlight-color: rgba(26, 26, 26, 0.296875); -webkit-composition-fill-color: rgba(175, 192, 227, 0.230469); -webkit-composition-frame-color: rgba(77, 128, 180, 0.230469); ">&nbsp;mobile)</span></div><div><br>On Apr 27, 2016, at 9:31 PM, Erica Sadun via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br><br></div><blockquote type="cite"><div><meta http-equiv="Content-Type" content="text/html charset=utf-8">On Apr 27, 2016, at 12:25 PM, Douglas Gregor &lt;<a href="mailto:dgregor@apple.com" class="">dgregor@apple.com</a>&gt; wrote:<br class=""><div><blockquote type="cite" class=""><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">On Apr 27, 2016, at 10:10 AM, Erica Sadun &lt;<a href="mailto:erica@ericasadun.com" class="">erica@ericasadun.com</a>&gt; wrote:<br class=""><div class=""><blockquote type="cite" class=""><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">From the Swift Programming Language:&nbsp;<i class="">Methods on a subclass that override the superclass’s implementation are marked with override—overriding a method by accident, without override, is detected by the compiler as an error. The compiler also detects methods with override that don’t actually override any method in the superclass.</i></div><div class=""><br class=""></div><div class="">I would like to extend this cautious approach to protocols, forcing the developer to deliberately override an implementation that’s inherited from a protocol extension. This would prevent accidental overrides and force the user to proactively choose to implement a version of a protocol member that already exists in the protocol extension.</div><div class=""><br class=""></div><div class="">I envision this as using the same `override` keyword that’s used in class based inheritance but extend it to protocol inheritance:</div><div class=""><br class=""></div><div class="">protocol A {</div><div class="">&nbsp; &nbsp; func foo()</div><div class="">}</div><div class=""><br class=""></div><div class="">extension A {</div><div class="">&nbsp; &nbsp; func foo() { .. default implementation … }</div><div class="">}</div><div class=""><br class=""></div><div class="">type B: A {</div><div class=""><br class=""></div><div class="">&nbsp; &nbsp; override required func foo () { … overrides implementation … }</div><div class="">}</div></div></div></blockquote><div class=""><br class=""></div><div class="">A couple questions about your pitch:</div><div class=""><br class=""></div><div class="">1) What is “required” doing there?&nbsp;</div></div></div></div></blockquote><div><br class=""></div>I threw it in not because I’m tied to it but because I wanted it to be part of the conversation.</div><div>This is a requirement from conforming to the protocol.</div><div><br class=""><blockquote type="cite" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><div class="">2) Is “override” only required when there is a default implementation of the protocol requirement, or is it required whenever you are implementing a protocol requirement?</div></div></div></blockquote><div><br class=""></div><div>Override is only because it is overriding the default implementation of the protocol requirement. Without that default implementation there would be no override, it would simply be satisfying the requirement.</div><br class=""><blockquote type="cite" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><div class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>*&nbsp;If the former, it might be the case that it’s too easy to forget to add the “override” keyword (because it’s needed for some implementations of protocol requirements but not others), which undercuts the value of having it.</div></div></div></blockquote><div><br class=""></div><div>Forcing the override keyword makes it clear at the definition point that the story extends beyond the method or whatever to point to a default implementation that is being replaced. I *really* like having that reference in terms of both code construction (“I am doing this as a deliberate act”) with the compiler complaining otherwise, and in terms of code self documentation (“I know this was added deliberately, what default did it override?”)</div><br class=""><blockquote type="cite" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><div class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>*&nbsp;If the latter, “override” is probably the wrong keyword because it’s not overriding anything in the common case of implementing a non-defaulted requirement.</div></div></div></blockquote><div><br class=""></div>It would be pointless if it’s just satisfying a requirement. That’s why &nbsp;introduced both keywords into the discussion. (And because I’m still being influenced by the “near miss” conversation.)</div><div><br class=""></div></div></blockquote><div><br></div><div>One could always argue that should protocol definitions ever be allowed to contain default implementations ala-java default methods, the distinction between former and latter would go away, and it would be happy anticipation to have mandated *override* all along in all cases, ensuring that future default methods would not accidentally take precedence of current code or wind up being treated differently than other overrides.</div><div><br></div><div><br></div><br><blockquote type="cite"><div><div><blockquote type="cite" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">I’d also like to bring up two related topics, although they probably should at some point move to their own thread if they have any legs:<br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><br class=""></div><div class="">Related topic 1: How should a consumer handle a situation where two unrelated protocols both require the same member and offer different default implementations. Can they specify which implementation to accept or somehow run both?&nbsp;</div><div class=""><br class=""></div><div class="">type B: A, C {</div><div class="">&nbsp; &nbsp; override required func foo() { A.foo(); C.foo() }</div><div class="">}</div></div></div></blockquote><div class=""><br class=""></div><div class="">I think the right answer here is for the compiler to produce an ambiguity if you don’t implement the requirement yourself, and then solving your “related topic 2” lets you choose which implementation you want.</div></div></div></blockquote><div><br class=""></div><div>How do you choose which one? What syntax? For example:</div><div><br class=""></div><div>required func foo = A.foo</div><div><br class=""></div><div>would be the simplest approach</div><br class=""><blockquote type="cite" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Related topic 2: How can a consumer “inherit” the behavior of the default implementation (like calling super.foo() in classes) and then extend that behavior further. This is a bit similar to how the initialization chaining works. I’d like to be able to call A.foo() and then add custom follow-on behavior rather than entirely replacing the behavior.</div><div class=""><br class=""></div><div class="">type B: A {</div><div class="">&nbsp; &nbsp; override required func foo() { A.foo(); … my custom behavior … }</div><div class="">}</div></div></div></blockquote><div class=""><br class=""></div><div class="">Seems totally reasonable to me. One ugly syntax: A.foo(self)(), leveraging the currying of self?</div></div></div></blockquote><div><br class=""></div><div>Ugly but it would pretty much do it for me. It offers an expressive way to say “Please execute the A.foo behavior using the self instance”. Does 3 still support this?</div><br class=""><blockquote type="cite" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">cc’ing in Jordan who suggested a new thread on this and Doug, who has already expressed some objections so I want him to &nbsp;have the opportunity to bring that discussion here.</div></div></div></blockquote><br class=""></div><div class="">My objections are described here:</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span><a href="http://thread.gmane.org/gmane.comp.lang.swift.devel/1799/focus=1831" class="">http://thread.gmane.org/gmane.comp.lang.swift.devel/1799/focus=1831</a></div><div class=""><br class=""></div><div class="">Essentially, my argument is that the point of “override” (as pitched above) is to declare the user’s intent to implement a requirement. I feel that the explicit protocol conformance ("type B : A”) declares that intent, and that various common conventions (e.g., one conformance per extension, where the extension is primarily there to conform to the protocol) reinforce intent well enough for the compiler to do a good job here. I’d prefer that over another boilerplate-y keyword.</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>- Doug</div></div></blockquote></div></div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></body></html>