I am not sure why some compiler hints use @ and others don&#39;t, but this feature seems closer in functionality to @available instead of final or required.<br><br>So I agree that overloading the required keyword seems out of place and using a different syntax (using @) seems more appropriate.<br><div class="gmail_quote"><div dir="ltr">On Wed, Feb 17, 2016 at 2:26 PM Kyle Sherman via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br></div><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>Thanks for the replies.</div><div><br></div><div>Kenny: After thinking about it more, discussing with Peter, and looking Haravikk’s comments, I think the best thing would be for this to be a warning as suggested. I respectfully disagree that as a library creator you would not be able to know that a call to super should be required. A perfect example of this is the one stated in the proposal: viewDidLoad, viewWillAppear, etc. In these cases, the library writers know that the super version must be called and no matter what the subclasser does, they will not be able to have correct behavior without calling super. This is present in many places throughout the iOS SDK as example. In the static analyzer in Xcode, for ObjC code, it warns when the developer doesn’t call super in certain cases. Having these annotations would allow for developers to specify this for their own code. Being able to suppress the warning could also be a good feature, but I definitely don’t feel it would be necessary for the implementation of the feature.</div><div><br></div><div>Haravikk: I don’t agree with using “(required)” as that will be a sort of overloaded usage of the keyword. Also, I think that simply not specifying anything would be a better way to go about it. So, @super would mean you require the super method to be called, and simply not having an annotation means that you do not have to call the super method. Peter and I have a separate proposal that we will put up here soon for extending the “required” keyword for all methods that I think will solve the problem of abstract classes. I don’t think that we should conflate that issue, which we feel is a separate issue, in with the issue of requiring the super method to be called. Along the same lines, final by default was already being discussed much earlier and I believe there was already a conclusion to that saying it would not be implemented. I think the final keyword should stay separate, especially since it is a compiler error vs this which we are now saying would be a warning.</div></div><div style="word-wrap:break-word"><div><br></div><div>-Kyle</div></div><div style="word-wrap:break-word"><div><br></div><div><blockquote type="cite"><div>On Feb 17, 2016, at 12:31 PM, Haravikk via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:</div><br><div><div style="word-wrap:break-word"><div>Since this proposal is along the same lines as another current thread I’m contributing to I’m of course very much for the basic functionality ;-)</div><div>From that other discussion my current preference is towards either an attribute named @super, or allow the super keyword to be used in the method declaration. So a declaration might look like:</div><div><br></div><div><font face="Monaco"><span style="white-space:pre-wrap">        </span>@super(required)</font></div><div><font face="Monaco"><span style="white-space:pre-wrap">        </span>func someMethod() { … }</font></div><div><br></div><div>Or (if you prefer):</div><div><br></div><div><font face="Monaco"><span style="white-space:pre-wrap">        </span>super(required) func someMethod() { … }</font></div><div><br></div><div>The main ones needed are required and optional, with optional being what we have now, and required simply ensuring that the super method is called (no requirement on where). Options for before and after will be more useful if we get abstract classes whose sole purpose is to be extended, as they may have more specific requirements. The other possibly useful option would @super(replace), in which case super may not be called by extending methods at all, as it’s implementation may be very tightly coupled to the specific implementation at that level, thus requiring a sub-class to re-implement it; the parent method could also be some kind of stub for a feature it doesn’t support, but which was required by a protocol for example.</div><div><br></div><div>Howard Lovatt also mentioned another interesting extension to the idea which is that methods would effectively become final by default, requiring a @super attribute if they are to be overridable, which I think would be good for ensuring that only classes designed with extension in mind can actually be extended. For this reason the @super attribute could also be used on a class to set a new default for all methods. If we go with this aspect then the final keyword would probably be moved into the @super attribute for methods (properties would still have access to it though I think).</div><br><div><blockquote type="cite"><div>On 17 Feb 2016, at 19:55, Kenny Leung via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:</div><div><br>There is just no way to know what may be required by clients of a framework when it comes time to write actual shipping code. To require users to call super - and to even extend that to knowing whether they should call super at the beginning or end of their methods is too restrictive. If this feature were to go forward, I would limit it to a warning. It may belong more in a linter.<br></div></blockquote><div><br></div><div>A lot of people have reacted negatively to the idea of before and after requirements, but I think it’s important to note that they’re unlikely to be commonly added; in most cases it won’t matter when the super method is called so required or optional should be used as appropriate. Before and after requirements are more useful for abstract or abstract-style classes that offer partial implementations specifically designed to be extended, in which case the order may be more important. Most of the time the deciding factor will be whether your method performs some kind of updates to cached values, in which case it will be important that it is called; the requirement can also be used to serve as a reminder to read the documentation about how exactly the parent method should be used by extending classes, if it has any more unusual caveats.</div><br><blockquote type="cite"><div>I’ve been using IntelliJ IDEA a lot lately, and they basically have a live linter that they call “code inspections”. I like this a lot. It goes waaay beyond compiler-level warnings to offering you suggestions to improve your code, finding sections of duplicated code, anything under the sun. They also allow you to suppress any individual warning by putting an @suppress in your code. Maybe Swift could benefit from another layer like this in general, where you could be warned about a lot of stuff, but not be locked into it.<br></div></blockquote><div><br></div><div>It could make sense to have an option for whether breaking a requirement produces a warning or an error? For example @super(required, warn) will warn the developer that they are going against the parent class’ requirement to include the super method, but won’t actually stop them from doing so if they really want to. This could provide a useful middle-ground between optional and required, plus if the code of the parent class isn’t under your control it gives you a fallback until there’s an update if the warning behaviour were the default, with @super(required, error) style declarations for developers who really know what their parent class needs from sub-classes.</div><div><br></div><div>It’s also worth considering that most code will be under your control, so you’ll just need to tweak (or add) the requirements, in the latter case it forces you to consider how that class may be extended which I think is a good thing.</div><br><blockquote type="cite"><div><blockquote type="cite">On Feb 17, 2016, at 10:02 AM, Kyle Sherman via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:<br><br>I just saw that there was a discussion started about this topic just recently while I was developing this idea with my colleague Peter Livesey. So, I figured I would submit this proposal for discussion.<br><br>The link to the original discussion is here: <a href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160215/010310.html" target="_blank">https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160215/010310.html</a><br><br>The subject was: “Replace the override keyword by ‘extend’ and ‘replace’ or add an annotation like @SuppressSuperCall”<br><br>-Kyle<br><br># Enforcing Calling Super<br><br>* Proposal: [SE-NNNN](<a href="https://github.com/apple/swift-evolution/blob/master/proposals/NNNN-name.md" target="_blank">https://github.com/apple/swift-evolution/blob/master/proposals/NNNN-name.md</a>)<br>* Author(s): [Swift Developer](<a href="https://github.com/swiftdev" target="_blank">https://github.com/swiftdev</a>)<br>* Status: **Awaiting review**<br>* Review manager: TBD<br><br>## Introduction<br><br>Many times when creating a subclass the superclass has reasons for certain overridden methods to call the superclass’s version of the method. This change would enforce that the subclass called the superclass&#39;s method in its overridden version at compile time. Also, it would optionally enforce that the superclass&#39;s version would be called before any other implementation in the method (similar to initialization rules). <br><br>Swift-evolution thread: [link to the discussion thread for that proposal](<a href="https://lists.swift.org/pipermail/swift-evolution" target="_blank">https://lists.swift.org/pipermail/swift-evolution</a>)<br><br>## Motivation<br><br>A concrete example of the type of problem this solves can be taken from simple iOS code. When creating a subclass of UIViewController, you often need to override methods like viewDidLoad or viewWillAppear. You are supposed to call super.viewDidLoad or super.viewWillAppear, respectively, in your overridden implementation. If you don&#39;t, you will have undefined behavior and run into issues. Of course, this type of situation can be extrapolated to any class created in Swift. <br><br>Currently, the only way this can be enforced is by commenting the superclass&#39;s code and making a note in the documentation. Quite obviously this can cause many issues as mistakes can be made by new developers quite easily who didn&#39;t look at the documentation for the method or even seasoned developers who simply overlooked this small detail. <br><br>## Proposed solution<br><br>The solution proposed here would be to use an annotation similar to @available and @noescape in order to convey this information. Optionally, the developer can also choose to specify that the super method must be called as the first line or last line of the overridden method. <br><br>The compiler would use the information from the annotation to ensure that any overridden version of the method must call super at the appropriate time according to the information given in the annotation. The compiler would also need to ensure that any method that was going to use this annotation had the same access control level as the class that contains it.<br><br>This solution will be much safer than what is currently available, because there is currently no way to enforce super being called in an overridden method. This bug happens constantly for iOS developers.<br><br>## Detailed design<br><br>A possible implementation of this may look like this:<br><br>```<br>class MyClass {<br>    @requiredSuper func foo1() { }<br><br>    @requiredSuper(start) func foo2() { }<br><br>    @requiredSuper(end) func foo3() { }<br>}<br>```<br><br>Now, if the developer were to create a subclass and not call the super method, the compiler should display an error. The errors that should be displayed should be similar to: <br><span style="white-space:pre-wrap">        </span>• Overridden method must call the superclass’s implementation<br><span style="white-space:pre-wrap">        </span>• Overridden method must call the superclass’s implementation as the first line of the method.<br><span style="white-space:pre-wrap">        </span>• Overridden method must call the superclass’s implementation as the last line of the method.<br>for the cases of `@requiredSuper`, `@requiredSuper(start)`, and `@requiredSuper(end)` respectively.<br><br>The compiler would also need to display an error in this case where the access control of the method is stricter than that of the class:<br><br>```<br>public class MyClass {<br>    @requiredSuper func foo() { }<br>}<br>```<br><br>The compiler should show an error, such as “A method using @requiredSuper must have access control set to be at least as accessible as the class that contains it”.<br><br>## Impact on existing code<br><br>Implementation of this feature by the developer is completely optional. Therefore, existing code will be unaffected and no migration of code will be necessary. However, when APIs are updated to use this new feature, some code will not compile if the developer did not use the APIs correctly. This should be a welcomed compilation error as it will result in less buggy code at runtime. The impact of this change is similar to adding nullability annotations to Objective-C.<br><br>It will be impossible to migrate code automatically, because this information cannot be derived in any way aside from reading comments if and only if the API author documented it.<br><br>## Alternatives considered<br><br>The alternative would simply be to not implement this feature.<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" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a></blockquote></div></blockquote></div><br></div>_______________________________________________<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>_______________________________________________<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>