<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 Aug 2, 2017, at 5:30 PM, Xiaodi Wu <<a href="mailto:xiaodi.wu@gmail.com" class="">xiaodi.wu@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class=""><div dir="auto" class="">Besides the comments below on consensus about how to deal with the documentation aspect, I should point out that your example is incorrect. != is not a protocol requirement of Equatable with a default implementation, but a protocol extension method that cannot be overridden which is *not* a protocol requirement. That is by design.</div><div dir="auto" class=""><br class=""></div><div dir="auto" class="">Additionally, it is unclear to me why default implementations should need be provided in the same file, as your solution requires. There are good reasons why library A that builds upon library B might supply a default implementation in an extension for a protocol B.P that refines B.Q. To the end user, it is just as much of a default that doesn’t need to be implemented when conforming to P, but your proposed solution does nothing to address this issue.</div><div dir="auto" class=""><br class=""></div></div></div></blockquote><div><br class=""></div><div>Yes, it is restrictive. My approach to open-ended proposals like this (ones that might have unforeseen consequences) is to start with the bare minimum and work outwards from there. If the default implementation is in the same file, then the meta-information about the protocol requirement can be safely cached after compiling an individual file and it would remain valid when linking against the rest of object files in the module. I may, however, be completely wrong in how this works, so that's why I encourage people to put the proposed limitations to question. By the way, thank you for doing that 🙂</div><br class=""><blockquote type="cite" class=""><div class=""><div class=""><br class=""><div class="gmail_quote"><div class="">On Wed, Aug 2, 2017 at 03:54 Gor Gyolchanyan via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class=""></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class="">Good day, swift community!<div class="">This time, I'd like to bring your attention to an everyday problem which, I'm sure, you've all dealt with at some point.</div><div class=""><br class=""></div><div class=""><b class="">Motivation</b></div><div class=""><br class=""></div><div class="">Many protocols have a lot of default-implemented requirements so that they can be customized, which would otherwise be impossible to do.</div><div class="">For example, the <font color="#4f7a28" face="Menlo" class="">Comparable</font> protocol, which (considering its inheritance of <font face="Menlo" color="#4f7a28" class="">Equatable</font>) states a requirement of <font face="Menlo" class="">==</font>, <font face="Menlo" class="">!=</font>, <font face="Menlo" class=""><</font>, <font face="Menlo" class=""><=</font>, <font face="Menlo" class="">></font>, and <font face="Menlo" class="">>=</font>, only requires implementation of <font face="Menlo" class="">==</font> and <font face="Menlo" class=""><</font>, because all others have default implementations in terms of these two. This forces the user to either memorize the exact set of mandatory requirements or, in case of more complicated protocols like <font face="Menlo" color="#4f7a28" class="">Collection</font>, constantly look up the documentation and hope that it has a section outlining how to conform to it.</div><div class="">Conversely, the author of the protocol needs to manually keep track of the requirements and their default implementations to ensure that the ones that have to be mandatory stay mandatory and the rest of the requirements stay default-implemented, which is also a tedious and error-prone work.</div><div class=""><br class=""></div><div class=""><b class="">Proposed Solution</b></div><div class=""><br class=""></div><div class="">I'd like to propose the ability to mark select protocol requirements as <font color="#b92d5d" face="Menlo" class="">default</font>, which would cause the following effects:</div><div class=""><ul class="m_-3206054945558180310MailOutline"><li class="">The compiler will emit a compile-time error if a default implementation is not available in a non-constrained protocol extension in the same file and provide a fix-it for inserting a template.</li><li class="">If a conforming type does not implement all requirements of the protocol, the compiler will not produce fix-its for inserting a template for any <font color="#b92d5d" face="Menlo" class="">default</font> requirements.</li></ul></div><div class=""><br class=""></div><div class=""><b class="">Example</b></div><div class=""><br class=""></div><div class=""><font face="Menlo" color="#7a7a7a" class="">// Equatable.swift</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><font face="Menlo" class=""><font color="#b92d5d" class="">protocol</font> <font color="#4f7a28" class="">Equatable</font> {</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><font face="Menlo" class=""><span class="m_-3206054945558180310Apple-tab-span" style="white-space:pre-wrap">        </span><font color="#b92d5d" class="">static</font> <font color="#b92d5d" class="">func</font> == (_ some: <font color="#b92d5d" class="">Self</font>, _ other: <font color="#b92d5d" class="">Self</font>) -> <font color="#4f7a28" class="">Bool</font></font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><font face="Menlo" class=""><span class="m_-3206054945558180310Apple-tab-span" style="white-space:pre-wrap">        </span><font color="#b92d5d" class="">default</font> <font color="#b92d5d" class="">static</font> <font color="#b92d5d" class="">func</font> != (_ some: <font color="#b92d5d" class="">Self</font>, _ other: <font color="#b92d5d" class="">Self</font>) -> <font color="#4f7a28" class="">Bool</font></font></div><div class=""><font face="Menlo" class=""><span class="m_-3206054945558180310Apple-tab-span" style="white-space:pre-wrap">        </span></font><font color="#e32400" class=""><i class=""><font style="font-family:Menlo" class="">^ error: missing implementation of a default requirement `!=`; </font><font style="font-family:Menlo" class="">fix-it: insert a default implementation</font></i></font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><font face="Menlo" class="">}</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><font face="Menlo" class=""><font color="#b92d5d" class="">extension</font> <font color="#4f7a28" class="">Equatable</font> {</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><font face="Menlo" class=""><span class="m_-3206054945558180310Apple-tab-span" style="white-space:pre-wrap">        </span></font><font color="#b92d5d" face="Menlo" class="">static</font><span style="font-family:Menlo" class=""> </span><font color="#b92d5d" face="Menlo" class="">func</font><span style="font-family:Menlo" class=""> != (_ some: </span><font color="#9a244f" style="font-family:Menlo" class="">Self</font><span style="font-family:Menlo" class="">, _ other: </span><font color="#9a244f" style="font-family:Menlo" class="">Self</font><span style="font-family:Menlo" class="">) -> </span><font color="#4f7a28" face="Menlo" class="">Bool</font><font face="Menlo" class=""> {</font></div><div class=""><font face="Menlo" class=""><span class="m_-3206054945558180310Apple-tab-span" style="white-space:pre-wrap">                </span><b class="">[<font color="#4d22b3" class="">Your Code Here]</font></b></font></div><div class=""><font face="Menlo" class=""><span class="m_-3206054945558180310Apple-tab-span" style="white-space:pre-wrap">        </span>}</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><font face="Menlo" class="">}</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><font face="Menlo" color="#929292" class="">// Something.swift</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><font face="Menlo" class=""><font color="#b92d5d" class="">struct</font> <font color="#4f7a28" class="">Something</font>: <font color="#4f7a28" class="">Equatable</font> {</font></div><div class=""><i class=""><font color="#e32400" style="font-family:Menlo" class="">^ error: missing implementation of a non-default requirement `==`; </font><font color="#e32400" style="font-family:Menlo" class="">fix-it: insert an implementation</font></i></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><font face="Menlo" class="">}</font></div><div class=""><br class=""></div><div class=""><div class=""><font face="Menlo" class=""><font color="#b92d5d" class="">extension</font> </font><font color="#4f7a28" face="Menlo" class="">Something</font><font face="Menlo" class=""> {</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><font face="Menlo" class=""><span class="m_-3206054945558180310Apple-tab-span" style="white-space:pre-wrap">        </span></font><font color="#b92d5d" face="Menlo" class="">static</font><span style="font-family:Menlo" class=""> </span><font color="#b92d5d" face="Menlo" class="">func</font><span style="font-family:Menlo" class=""> == (_ some: </span><font color="#4f7a28" face="Menlo" class="">Something</font><span style="font-family:Menlo" class="">, _ other: </span><font color="#4f7a28" face="Menlo" class="">Something</font><span style="font-family:Menlo" class="">) -> </span><font color="#4f7a28" face="Menlo" class="">Bool</font><font face="Menlo" class=""> {</font></div><div class=""><font face="Menlo" class=""><span class="m_-3206054945558180310Apple-tab-span" style="white-space:pre-wrap">                </span><b class="">[<font color="#4d22b3" class="">Your Code Here]</font></b></font></div><div class=""><font face="Menlo" class=""><span class="m_-3206054945558180310Apple-tab-span" style="white-space:pre-wrap">        </span>}</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><font face="Menlo" class="">}</font></div></div><div class=""><br class=""></div><div class="">Cheers,</div><div class="">Gor Gyolchanyan.</div></div>_______________________________________________<br class="">
swift-evolution mailing list<br class="">
<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
</blockquote></div></div>
</div></blockquote></div><br class=""></body></html>