<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="">Great catch Jeremy. I'm not sure how the availability API works with other annotations. Does anyone know if this is possible?<div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Mar 1, 2016, at 7:31 AM, Jeremy W. Sherman via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="content-type" content="text/html; charset=utf-8" class=""><div dir="auto" class=""><div class="">Ah bother, the list doesn't default reply-to to the list? (Or iOS mail doesn't respect the header.)<br class="">--<div class="">Jeremy W. Sherman</div><div class=""><a href="http://jeremywsherman.com/" class="">http://jeremywsherman.com/</a></div></div><div class=""><br class="">Inicio del mensaje reenviado:<br class=""><br class=""></div><blockquote type="cite" class=""><div class=""><b class="">De:</b> "Jeremy W. Sherman" <<a href="mailto:jeremyw.sherman@gmail.com" class="">jeremyw.sherman@gmail.com</a>><br class=""><b class="">Fecha:</b> 1 de marzo de 2016, 10:30:05 GMT-5<br class=""><b class="">Para:</b> Kyle Sherman <<a href="mailto:kyledsherman@gmail.com" class="">kyledsherman@gmail.com</a>><br class=""><b class="">Asunto:</b> <b class="">Re: [swift-evolution] [Discussion] Enforcing Calling Super</b><br class=""><br class=""></div></blockquote><blockquote type="cite" class=""><div class=""><meta http-equiv="content-type" content="text/html; charset=utf-8" class=""><div class="">Does this handle the case of required super only since a version?</div><div class=""><br class=""></div><div class="">I recall NSView or something like that gaining an awakeFromNib implementation late, say 10.5 or so, so that one had to conditionalize calls to [super awakeFromNib] based on whether or not the superclass's instances responded to that selector. If your code was running on 10.4, calling super would crash with unrecognized selector; if on 10.5, you were always supposed to call super.</div><div class=""><br class=""></div><div class="">This suggests some interaction between requiring calls to super and availability annotations at the least.<br class="">--<div class="">Jeremy W. Sherman</div><div class=""><a href="http://jeremywsherman.com/" class="">http://jeremywsherman.com/</a></div></div><div class=""><br class="">El 17-02-2016, a las 13:02, Kyle Sherman via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> escribió:<br class=""><br class=""></div><blockquote type="cite" class=""><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><span id="docs-internal-guid-a38d8475-f060-9ecd-2dc3-ffebf5051cbc" class=""><div style="line-height: 1.38; margin-top: 0pt; margin-bottom: 0pt;" class=""><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class="">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.</span></div><div style="line-height: 1.38; margin-top: 0pt; margin-bottom: 0pt;" class=""><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class=""><br class=""></span></div><div style="line-height: 1.38; margin-top: 0pt; margin-bottom: 0pt;" class=""><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class="">The link to the original discussion is here: <a href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160215/010310.html" class="">https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160215/010310.html</a></span></div><div style="line-height: 1.38; margin-top: 0pt; margin-bottom: 0pt;" class=""><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class=""><br class=""></span></div><div style="line-height: 1.38; margin-top: 0pt; margin-bottom: 0pt;" class=""><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class="">The subject was: “Replace the override keyword by ‘extend’ and ‘replace’ or add an annotation like @SuppressSuperCall”</span></div><div style="line-height: 1.38; margin-top: 0pt; margin-bottom: 0pt;" class=""><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class=""><br class=""></span></div><div style="line-height: 1.38; margin-top: 0pt; margin-bottom: 0pt;" class=""><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class="">-Kyle</span></div><div style="line-height: 1.38; margin-top: 0pt; margin-bottom: 0pt;" class=""><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class=""><br class=""></span></div><div style="line-height: 1.38; margin-top: 0pt; margin-bottom: 0pt;" class=""><span id="docs-internal-guid-a38d8475-f065-cb3e-e738-20c79773e867" class=""><div style="line-height: 1.38; margin-top: 0pt; margin-bottom: 0pt;" class=""><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class=""># Enforcing Calling Super</span><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class=""><br class="kix-line-break"></span><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class=""><br class="kix-line-break"></span><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class="">* Proposal: [SE-NNNN](<a href="https://github.com/apple/swift-evolution/blob/master/proposals/NNNN-name.md" class="">https://github.com/apple/swift-evolution/blob/master/proposals/NNNN-name.md</a>)</span><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class=""><br class="kix-line-break"></span><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class="">* Author(s): [Swift Developer](<a href="https://github.com/swiftdev" class="">https://github.com/swiftdev</a>)</span><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class=""><br class="kix-line-break"></span><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class="">* Status: **Awaiting review**</span><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class=""><br class="kix-line-break"></span><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class="">* Review manager: TBD</span><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class=""><br class="kix-line-break"></span><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class=""><br class="kix-line-break"></span><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class="">## Introduction</span><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class=""><br class="kix-line-break"></span><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class=""><br class="kix-line-break"></span><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class="">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's method in its overridden version at compile time. Also, it would optionally enforce that the superclass's version would be called before any other implementation in the method (similar to initialization rules). </span></div><div style="line-height: 1.38; margin-top: 0pt; margin-bottom: 0pt;" class=""><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class=""><br class="kix-line-break"></span><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class="">Swift-evolution thread: [link to the discussion thread for that proposal](<a href="https://lists.swift.org/pipermail/swift-evolution" class="">https://lists.swift.org/pipermail/swift-evolution</a>)</span><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class=""><br class="kix-line-break"><br class="kix-line-break"></span></div><div style="line-height: 1.38; margin-top: 0pt; margin-bottom: 0pt;" class=""><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class="">## Motivation</span><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class=""><br class="kix-line-break"></span><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class=""><br class="kix-line-break"></span><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class="">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'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. </span></div><br class=""><div style="line-height: 1.38; margin-top: 0pt; margin-bottom: 0pt;" class=""><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class="">Currently, the only way this can be enforced is by commenting the superclass'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't look at the documentation for the method or even seasoned developers who simply overlooked this small detail. </span></div><br class=""><div style="line-height: 1.38; margin-top: 0pt; margin-bottom: 0pt;" class=""><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class="">## Proposed solution</span><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class=""><br class="kix-line-break"><br class="kix-line-break"></span></div><div style="line-height: 1.38; margin-top: 0pt; margin-bottom: 0pt;" class=""><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class="">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. </span></div><br class=""><div style="line-height: 1.38; margin-top: 0pt; margin-bottom: 0pt;" class=""><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class="">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.</span></div><br class=""><div style="line-height: 1.38; margin-top: 0pt; margin-bottom: 0pt;" class=""><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class="">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.</span><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class=""><br class="kix-line-break"></span><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class=""><br class="kix-line-break"></span><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class="">## Detailed design</span><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class=""><br class="kix-line-break"><br class="kix-line-break"></span></div><div style="line-height: 1.38; margin-top: 0pt; margin-bottom: 0pt;" class=""><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class="">A possible implementation of this may look like this:</span></div><br class=""><div style="line-height: 1.38; margin-top: 0pt; margin-bottom: 0pt;" class=""><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class="">```</span></div><div style="line-height: 1.38; margin-top: 0pt; margin-bottom: 0pt;" class=""><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class="">class MyClass {</span></div><div style="line-height: 1.38; margin-top: 0pt; margin-bottom: 0pt;" class=""><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class=""> @requiredSuper func foo1() { }</span></div><br class=""><div style="line-height: 1.38; margin-top: 0pt; margin-bottom: 0pt;" class=""><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class=""> @requiredSuper(start) func foo2() { }</span></div><br class=""><div style="line-height: 1.38; margin-top: 0pt; margin-bottom: 0pt;" class=""><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class=""> @requiredSuper(end) func foo3() { }</span></div><div style="line-height: 1.38; margin-top: 0pt; margin-bottom: 0pt;" class=""><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class="">}</span></div><div style="line-height: 1.38; margin-top: 0pt; margin-bottom: 0pt;" class=""><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class="">```</span></div><br class=""><div style="line-height: 1.38; margin-top: 0pt; margin-bottom: 0pt;" class=""><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class="">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: </span></div><ol style="margin-top:0pt;margin-bottom:0pt;" class=""><li dir="ltr" style="list-style-type: decimal; font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline;" class=""><div style="line-height: 1.38; margin-top: 0pt; margin-bottom: 0pt;" class=""><span style="font-size: 14.666666666666666px; vertical-align: baseline; white-space: pre-wrap;" class="">Overridden method must call the superclass’s implementation</span></div></li><li dir="ltr" style="list-style-type: decimal; font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline;" class=""><div style="line-height: 1.38; margin-top: 0pt; margin-bottom: 0pt;" class=""><span style="font-size: 14.666666666666666px; vertical-align: baseline; white-space: pre-wrap;" class="">Overridden method must call the superclass’s implementation as the first line of the method.</span></div></li><li dir="ltr" style="list-style-type: decimal; font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline;" class=""><div style="line-height: 1.38; margin-top: 0pt; margin-bottom: 0pt;" class=""><span style="font-size: 14.666666666666666px; vertical-align: baseline; white-space: pre-wrap;" class="">Overridden method must call the superclass’s implementation as the last line of the method.</span></div></li></ol><div style="line-height: 1.38; margin-top: 0pt; margin-bottom: 0pt;" class=""><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class="">for the cases of `@requiredSuper`, `@requiredSuper(start)`, and `@requiredSuper(end)` respectively.</span></div><br class=""><div style="line-height: 1.38; margin-top: 0pt; margin-bottom: 0pt;" class=""><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class="">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:</span></div><br class=""><div style="line-height: 1.38; margin-top: 0pt; margin-bottom: 0pt;" class=""><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class="">```</span></div><div style="line-height: 1.38; margin-top: 0pt; margin-bottom: 0pt;" class=""><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class="">public class MyClass {</span></div><div style="line-height: 1.38; margin-top: 0pt; margin-bottom: 0pt;" class=""><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class=""> @requiredSuper func foo() { }</span></div><div style="line-height: 1.38; margin-top: 0pt; margin-bottom: 0pt;" class=""><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class="">}</span></div><div style="line-height: 1.38; margin-top: 0pt; margin-bottom: 0pt;" class=""><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class="">```</span></div><br class=""><div style="line-height: 1.38; margin-top: 0pt; margin-bottom: 0pt;" class=""><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class="">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”.</span></div><div style="line-height: 1.38; margin-top: 0pt; margin-bottom: 0pt;" class=""><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class=""><br class="kix-line-break"></span><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class="">## Impact on existing code</span><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class=""><br class="kix-line-break"></span><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class=""><br class="kix-line-break"></span><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class="">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.</span></div><br class=""><div style="line-height: 1.38; margin-top: 0pt; margin-bottom: 0pt;" class=""><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class="">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.</span><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class=""><br class="kix-line-break"></span><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class=""><br class="kix-line-break"></span><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class="">## Alternatives considered</span></div><br class=""><div style="line-height: 1.38; margin-top: 0pt; margin-bottom: 0pt;" class=""><span style="font-size: 14.666666666666666px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap;" class="">The alternative would simply be to not implement this feature.</span></div><br class=""><br class=""></span></div></span></div></blockquote><blockquote type="cite" class=""><div class=""><span class="">_______________________________________________</span><br class=""><span class="">swift-evolution mailing list</span><br class=""><span class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a></span><br class=""><span class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br class=""></div></blockquote></div></blockquote></div>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></div></body></html>