<div dir="ltr">+1 to @super :-)</div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Feb 15, 2016 at 8:52 PM, Haravikk via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div>This is an interesting idea, and fits well with the theme of preventing mistakes in Swift, but I think that the proposed solution isn’t flexible enough, as there are cases for inheritance patterns where extending doesn’t actually make sense, so having to specify an exception every time could quickly become annoying.</div><div><br></div><div>I think the better solution is to instead allow super-classes to specify whether or not their method must be called when overridden/extended. For example:</div><div><br></div><div><font face="Monaco"><span style="white-space:pre-wrap">        </span>class View {</font></div><div><font face="Monaco"><span style="white-space:pre-wrap">                </span>@super(required) func viewDidLoad() { … }</font></div><div><font face="Monaco"><span style="white-space:pre-wrap">        </span>}</font></div><div><font face="Monaco"><br></font></div><div><font face="Monaco"><span style="white-space:pre-wrap">        </span>class Button : View {</font></div><div><font face="Monaco"><span style="white-space:pre-wrap">                </span>override func viewDidLoad() { … }</font></div><div><font face="Monaco"><span style="white-space:pre-wrap">        </span>}</font></div><div><font face="Monaco"><br></font></div><div><font face="Monaco"><span style="white-space:pre-wrap">        </span>class Widget : View {</font></div><div><font face="Monaco"><span style="white-space:pre-wrap">                </span>override func viewDidLoad() {</font></div><div><font face="Monaco"><span style="white-space:pre-wrap">                        </span>super.viewDidLoad()</font></div><div><font face="Monaco"><span style="white-space:pre-wrap">                        </span>…</font></div><div><font face="Monaco"><span style="white-space:pre-wrap">                </span>}</font></div><div><font face="Monaco"><span style="white-space:pre-wrap">        </span>}</font></div><div><br></div><div>In this extension of your example Button will cause an error because it overrides viewDidLoad() but fails to call the parent’s method as required by the @super attribute. However, Widget compiles successfully because it follows the requirement.</div><div><br></div><div>So the options for @super would be:</div><div><br></div><div><ul><li><b>required: </b>child-class must call parent implementation of this method.</li><li><b>optional:</b> default behaviour, child can choose whether to call the parent’s method.</li><li><b>denied:</b> child may not call parent’s method (useful if it makes assumptions that a child-class may not follow), but can still extend/override.</li></ul><div><br></div></div><div>I think this would be a more flexible solution to the problem, and put the decision in the hands of those writing classes designed for inheritance. I’m not 100% sure of whether to rename override to extend, I like extend better personally, but it probably doesn’t matter overall.</div><div><div class="h5"><br><div><blockquote type="cite"><div>On 15 Feb 2016, at 20:57, Florian Liefers via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:</div><br><div>Hi!<br><br>I would like to suggest to replace the override keyword for functions by something like extend and replace or to add an annotation like @SuppressSuperCall (don’t know a good name for it).<br>The reason for this is, that it might happen, that one forgets to call the super’s implementation in an overridden function or if one reads the code it might not be obvious why the super’s implementation is not called:<br><br>class View {<br>   func viewDidLoad() {<br>      // does something<br>   }<br>}<br><br>class Button: View {<br>  override func viewDidLoad() {<br>      super.viewDidLoad()   // &lt;— this might be forgotten<br>      // do something other<br>   }<br>}<br><br>The compiler will accept if one overrides a superclass’s function but does not call the superclass’s implementation which is often ok. The developer should clearly state that he doesn’t want to call the superclass’s implementation, otherwise the compiler should throw an error.<br><br>// Example for extending a function<br>class Button: View {<br>  extend func viewDidLoad() {<br>      super.viewDidLoad()<br>      // do something<br>   }<br><br>  extend func viewDidAppear() {<br>      // do something<br>   } // &lt;— the compiler should throw an error here.<br>}<br><br>// Example for replacing a function<br>class Geometry {<br>   func volume() -&gt; Double {<br>      return 0;<br>   }<br>}<br><br>class Cube: Geometry {<br>   var length: Double = 0.0<br>   replace func volume() -&gt; Double {<br>      let v = length * length * length<br>      return v<br>   }<br>}<br><br>Cheers,<br>Florian<br>_______________________________________________<br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br></div></blockquote></div><br></div></div></div><br>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br></blockquote></div><br></div>