A lot of the time Cocoa documentation says that you must override a method, or must not, and if you do, whether to to it in the beginning, the end, or at some point. This proposal would make it enforceable by the compiler. With the right defaults, this could function exactly as what we have today but give additional ways for the compiler to enforce and automate the implementor's intent instead of relying on the documentation.<br><div class="gmail_quote"><div dir="ltr">On Tue, Feb 16, 2016 at 10:00 AM Alexey Demedetskiy via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I understand what you are talking about.<br>
But point is not to limit available behaviors, but to give some hints to compiler and protect programmer from errors.<br>
<br>
>From my point of view, there is no need to specify order in super class.<br>
But this can be helpful in child classes. I mean, that programmer will define the semantic of overriding, and not the semantic of inheritance.<br>
<br>
> I disagree with the introduction of a method to specify if super must be call first or last. Defining that super must be call is fine, but I don’t see why the operation order should be enforced.<br>
><br>
> If the super class declares a method overridable but require that some code must be perform first, it can simply execute that code before calling the overridable method instead of putting it in the super class definition then force the subclass to call super first.<br>
><br>
> Moreover, I’m not fond of declaring that requirement in the overriding classes. That is the superclass that should define if a super implementation is required, not the children classes.<br>
><br>
><br>
> > Le 16 févr. 2016 à 15:21, Ross O'Brien via swift-evolution<<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>(mailto:<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>)>a écrit :<br>
> > Jean-Daniel: can you clarify which aspect you're disagreeing with?<br>
> ><br>
> > For example: should standard library types prefer the 'override' form which enforces calling super but doesn't enforce when it is called?<br>
> > Perhaps 'override(before)' should be an indication that, if the overriding method doesn't explicitly declare the super call, then it should be called implicitly at the start of the function?<br>
> ><br>
> ><br>
> > On Tue, Feb 16, 2016 at 2:12 PM, Jean-Daniel Dupas via swift-evolution<<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>(mailto:<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>)>wrote:<br>
> > ><br>
> > > -1 for enforcing the super call place.<br>
> > > While we usually call super at the beginning of viewDidLoad for instance, there is absolutely no need to enforce it and it may result in problem when some work have to be done before the super method is executed.<br>
> > ><br>
> > > I have some use cases that rely on performing setup before calling the super.viewDidLoad().<br>
> > ><br>
> > > >Le 15 févr. 2016 à 23:06, Alexey Demedetskiy via swift-evolution<<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>(mailto:<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>)>a écrit :<br>
> > > ><br>
> > > >Hi<br>
> > > ><br>
> > > >I would like to suggest you to extend your proposal.<br>
> > > ><br>
> > > >In my practice, overriding super functions can have several semantics.<br>
> > > >1) Replace - simple case for abstract classes which implementation do nothing, or throw an exceptions.<br>
> > > >2) After super - things like viewDidLoad and viewWillAppear, setUp etc. All cases where super expect to be called before child code.<br>
> > > >3) Before super - opposite to 2.<br>
> > > >4) Override - no rules about order, but super call must be done.<br>
> > > ><br>
> > > >So code can look like:<br>
> > > ><br>
> > > >override(after) func viewDidLoad() {<br>
> > > >// super.viewDidLoad()<— no need to call super at first line.<br>
> > > >// child code<br>
> > > >}<br>
> > > ><br>
> > > >override(before) func tearDown() {<br>
> > > >// clean code<br>
> > > >// super… inserted by compiler<br>
> > > >}<br>
> > > ><br>
> > > >override(instead) func loadView() {<br>
> > > >// super.loadView()<— marked as an error with appropriate fix-up to remove instead modifier<br>
> > > >}<br>
> > > ><br>
> > > >override func refillHealthBar() {<br>
> > > >// absent call to super will cause an error with fix-up to add (instead) modifier<br>
> > > >}<br>
> > > ><br>
> > > >I am not sure about exposing this in a public interface and limit child override options.<br>
> > > ><br>
> > > >But in general - what is your thoughts about this approach to problem that you mention?<br>
> > > ><br>
> > > ><br>
> > > >>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() //<— 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>
> > > >>} //<— the compiler should throw an error here.<br>
> > > >>}<br>
> > > >><br>
> > > >>// Example for replacing a function<br>
> > > >>class Geometry {<br>
> > > >>func volume() ->Double {<br>
> > > >>return 0;<br>
> > > >>}<br>
> > > >>}<br>
> > > >><br>
> > > >>class Cube: Geometry {<br>
> > > >>var length: Double = 0.0<br>
> > > >>replace func volume() ->Double {<br>
> > > >>let v = length * length * length<br>
> > > >>return v<br>
> > > >>}<br>
> > > >>}<br>
> > > >><br>
> > > >>Cheers,<br>
> > > >>Florian<br>
> > > >><br>
> > > >><br>
> > > >><br>
> > > >_______________________________________________<br>
> > > >swift-evolution mailing list<br>
> > > ><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>(mailto:<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" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
> > ><br>
> > > _______________________________________________<br>
> > > swift-evolution mailing list<br>
> > > <a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>(mailto:<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" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
> ><br>
> > _______________________________________________<br>
> > swift-evolution mailing list<br>
> > <a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>(mailto:<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" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
><br>
><br>
><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" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</blockquote></div>