<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 17 Feb 2016, at 08:06, Jeremy Pereira via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><div class=""><br class="">I’m sorry, I don’t believe this. How can the implementor of the superclass possibly know whether the arbitrary code that the implementor of a subclass can write should go before, after or around the call to the super implementation? It’s nonsense.<br class=""></div></blockquote><div><br class=""></div><div>The before and after clauses probably wouldn’t be the most commonly used except in cases where the developer is very sure that they are needed. I think they’d be used more by abstract classes than regular ones, where a partial implementation needs to be completed in a particular way, required and optional would be the most commonly used I think.</div><br class=""><blockquote type="cite" class=""><div class="">They couldn’t call out to a private method to call the super implementation.</div></blockquote><div><br class=""></div><div>What do you hope to gain by doing so? If the parent method requires that it is called then it has to called in all paths the sub-class implementation can take. You can still outsource work to a private method, you can even still make a decision based on what the private method’s results are, so long as you call the super method in every case.</div><br class=""><blockquote type="cite" class=""><div class="">They couldn’t put it inside a loop or control structure unless the compiler can definitely prove that the code path is executed.</div></blockquote><div><br class=""></div><div>Why would you want to potentially call the super-method zero or many times when your child method is only called once? I’d also say that this is exactly what required should prevent, as there should be no uncertainty over it being called, since the parent method specifically forbids it.</div><div><br class=""></div><div>It shouldn’t prevent the use of a conditional, so long as all paths call the super method, i.e:</div><div><br class=""></div><div><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>if someCondition {</font></div><div><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>…</font></div><div><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>super.doSomething()</font></div><div><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>} else {</font></div><div><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>super.doSomething()</font></div><div><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>…</font></div><div><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</font></div><div><br class=""></div><div>Would be fine (both branches call the super method) but a conditional where super is only called in some branches, and nowhere else, is exactly what required is for. You actually raise an interesting question of whether it should be possible to call the super method more than once, that might be a possibility for another requirement, as in most cases it won’t make sense to call it more than once; for example, if you’re extending an add() method then potentially calling super.add() more than once seems like it could lead to unexpected results, and could be an error. For example:</div><div><br class=""></div><div><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>if someCondition {</font></div><div><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>…</font></div><div><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>super.doSomething()</font></div><div><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</font></div><div><font face="Monaco" class=""><br class=""></font></div><div><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>…</font></div><div><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>super.doSomething()</font></div><div><br class=""></div><div>Could be a bug where the developer has forgotten to remove the conditional super call; if the parent class knows its method shouldn’t be called more than once per child call then it could prevent this.</div><br class=""><blockquote type="cite" class=""><div class="">How do you deal with multi-level hierarchies? Do you enforce adding the attribute to the method in the intermediate class? <br class=""></div></blockquote><div><br class=""></div><div>It’s up to the developer of the sub-class. As proposed all methods would be final by default, including those overriding a method in a super class, so they would need their own requirements set in order for them to be overridable in the first place.</div><div><br class=""></div><div>For example:</div><div><br class=""></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>class MyClass {</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>@super(require)</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>func someMethod() { … }</div><div><br class=""></div><div><span class="Apple-tab-span" style="white-space:pre">                </span>@super(require)</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>func someOtherMethod() { … }</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div><br class=""></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>class MySubClass : MyClass {</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>@super(require)</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>override&nbsp;func someMethod() { … } // Can be extended further</div><div><br class=""></div><div><span class="Apple-tab-span" style="white-space:pre">                </span>override&nbsp;func someOtherMethod() { … } // Cannot be extended further</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><br class=""><blockquote type="cite" class=""><div class="">There will be enough edge cases and implementation issues to have subclass implementors cursing it.</div></blockquote><br class=""></div><div>Only if the developer of the parent class is over zealous in their choice of restrictions, but you could say the same thing about a bunch of features, like developers who mark almost everything private; like I say, the before and after requirements shouldn’t be very common, while the rest are pretty straightforward. In most cases you’ll either be extending a class that was designed for that purpose, in which case the requirements should be set correctly, or you will be extending a class in your own code base, so can request a change to the requirements (or change them yourself).</div></body></html>