<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=""><div class="">I quite knew about the caveat you’re talking about. This is what had prevented me from shipping this technique in the public API yet. I still need thinking.</div><div class=""><br class=""></div><div class="">And my current thinking is that this case is unlikely to happen (in my particular case, at least). Further, I’d rather document against this usage or even cutting off this feature, rather than adding this extra _f function. Talk about a simple API, when the library users needs a PhD in Swift dispatch subtleties to properly validate a poor struct before storing it in the database. That’s not my ambition when I write a library.</div><div class=""><br class=""></div><div class="">I hope, nevertheless, that I had shown:</div><div class=""><br class=""></div><div class="">1. that protocol default implementation can actually be "overriden", even if it is dangerous. If this danger can not be alleviated, then this is a hole in the language, and this hole may well need to be fixed because the inadequate usage I’ve show, should it reveal actually improper, will be discovered by others developers.</div><div class=""><br class=""></div><div class="">2. that there is a quite valid use case for letting adopting types "override" the default implementation of their protocol. The _f extra method is a lacking workaround.</div><div class=""><br class=""></div><div class="">Gwendal</div><div class=""><br class=""></div><div><blockquote type="cite" class=""><div class="">Le 9 déc. 2015 à 22:13, Kevin Ballard &lt;<a href="mailto:kevin@sb.org" class="">kevin@sb.org</a>&gt; a écrit :</div><br class="Apple-interchange-newline"><div class="">


<title class=""></title>

<div class=""><div class="">That is nice, but if someone writes a method that's generic over &lt;T: P&gt; then your "override" won't get called. Seems like it's better to structure it like<br class=""></div>
<div class="">&nbsp;</div>
<div class=""><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">protocol P {</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br class=""></span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp; &nbsp; func f()</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br class=""></span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">}</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br class=""></span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">extension P {</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br class=""></span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp; &nbsp; /// Default implementation for `f`. Calls through to `_f()`.</span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp; &nbsp; func f() { _f() }</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br class=""></span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp; &nbsp; /// Helper that provides the base functionality for `f`.</span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp; &nbsp; func _f() { ... }</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br class=""></span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">}</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br class=""></span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">struct S {</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br class=""></span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp; &nbsp; func f() {</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br class=""></span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp; &nbsp; &nbsp; &nbsp; ...</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br class=""></span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp; &nbsp; &nbsp; &nbsp; _f()</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br class=""></span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp; &nbsp; }</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br class=""></span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">}</span><br class=""></div>
<div class="">&nbsp;</div>
<div class="">This way you can write code that's generic over &lt;T: P&gt; (or that takes a P object directly) and it will still call the overrides.<br class=""></div>
<div class="">&nbsp;</div>
<div class="">-Kevin Ballard</div>
<div class="">&nbsp;</div>
<div class="">On Wed, Dec 9, 2015, at 11:01 AM, Gwendal Roué wrote:<br class=""></div>
<blockquote type="cite" class=""><div class="">&nbsp;</div>
<div class=""><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 :<br class=""></div>
<div class="">&nbsp;</div>
<div class=""><span class="font" style="font-family:Helvetica"><span class="size" style="font-size:12px">b) methods defined in protocol extensions by definition can't be overridden already,</span></span><br class=""></div>
</blockquote></div>
<div class="">&nbsp;</div>
<div class="">Methods defined in protocol extension actually can, sort of, be overridden, and this is very useful:<br class=""></div>
<div class="">&nbsp;</div>
<div class="">&nbsp; &nbsp; protocol P { }<br class=""></div>
<div class="">&nbsp; &nbsp; extension P {<br class=""></div>
<div class="">&nbsp; &nbsp; &nbsp; &nbsp; func f() { … }<br class=""></div>
<div class="">&nbsp; &nbsp; }<br class=""></div>
<div class="">&nbsp; &nbsp; struct S {<br class=""></div>
<div class="">&nbsp; &nbsp; &nbsp; &nbsp; func f() {<br class=""></div>
<div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...<br class=""></div>
<div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (self as P).f()<br class=""></div>
<div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; …<br class=""></div>
<div class="">&nbsp; &nbsp; &nbsp; &nbsp; }<br class=""></div>
<div class="">&nbsp; &nbsp; }<br class=""></div>
<div class="">&nbsp;</div>
<div class="">I know only one use case for this technique, in the groue/GRDB.swift SQLite wrapper:<br class=""></div>
<div class="">&nbsp;</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).<br class=""></div>
<div class="">&nbsp;</div>
<div class="">The benefits of this architecture are:<br class=""></div>
<div class="">&nbsp;</div>
<div class="">- You can subclass use the full-featured Record base class, and get CRUD + change tracking for free.<br class=""></div>
<div class="">- The Record subclasses can override the CRUD methods, and add custom code (validation, for example).<br class=""></div>
<div class="">- You can have a custom struct adopt DatabasePersistable, and get CRUD for free.<br class=""></div>
<div class="">- The custom structs that can also "override"&nbsp;the CRUD methods, and add custom code (validation, for example).<br class=""></div>
<div class="">&nbsp;</div>
<div class="">This is, in my opinion, a very valid use case for this "overriding".<br class=""></div>
<div class="">Gwendal Roué<br class=""></div>
</blockquote><div class="">&nbsp;</div>
</div>

</div></blockquote></div><br class=""></body></html>