<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="">Le 9 déc. 2015 à 19:52, Kevin Ballard via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; a écrit :</div><br class="Apple-interchange-newline"><div class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">b) methods defined in protocol extensions by definition can't be overridden already,</span></div></blockquote></div><br class=""><div class="">Methods defined in protocol extension actually can, sort of, be overridden, and this is very useful:</div><div class=""><br class=""></div><div class="">&nbsp; &nbsp; protocol P { }</div><div class="">&nbsp; &nbsp; extension P {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; func f() { … }</div><div class="">&nbsp; &nbsp; }</div><div class="">&nbsp; &nbsp; struct S {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; func f() {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (self as P).f()</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; …</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div class="">&nbsp; &nbsp; }</div><div class=""><br class=""></div><div class="">I know only one use case for this technique, in the groue/GRDB.swift SQLite wrapper:</div><div class=""><br class=""></div><div class="">In this library, a DatabasePersistable protocol provides basic CRUD operations, and a Record class adopts this protocol and "overrides" with the technique above the protocol methods with extra features provided by the class (especially change tracking).</div><div class=""><br class=""></div><div class="">The benefits of this architecture are:</div><div class=""><br class=""></div><div class="">- You can subclass use the full-featured Record base class, and get CRUD + change tracking for free.</div><div class="">- The Record subclasses can override the CRUD methods, and add custom code (validation, for example).</div><div class="">- You can have a custom struct adopt DatabasePersistable, and get CRUD for free.</div><div class="">- The custom structs that can also "override"&nbsp;the CRUD methods, and add custom code (validation, for example).</div><div class=""><br class=""></div><div class="">This is, in my opinion, a very valid use case for this "overriding".</div><div class="">Gwendal Roué</div></body></html>