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