[swift-evolution] Add code to super methods.

Aron Lindberg aron_lindberg at apple.com
Tue Nov 29 10:55:30 CST 2016


While I agree with most of your points Dave, I think forgetting to call super is very much a (hard to debug) real world problem today. Especially new developers seems unsure or likely to forget to call super when it is required.

I think my point is this; When you subclass code you have not written yourself you have no idea if the class you are subclassing is "poorly designed" and you should call super somewhere in your documentation. You can make assumptions, read the documentation or use trial and error, but there is no way to be certain unless the documentation is specific about it.

I went over the documentation for UIViewController. Is contains:
- 11 functions that specify that your subclass must call super during the implementation.
- 2 functions where the documentation specifies the default implementation is empty.
- 2 functions where you must not call super.
- 8 functions where you properly want to call super.
- 23 functions where I think there is no need to call super.

Based on the above I will admit that the default behaviour should properly be what we have today, but it would be lovely to have compiler warnings for the 11 functions that require it and the 2 functions where we should not call super.

> On 29 Nov 2016, at 16.05, Dave Abrahams via swift-evolution <swift-evolution at swift.org> wrote:
> 
> On Nov 29, 2016, at 1:48 AM, Tino Heth <2th at gmx.de <mailto:2th at gmx.de>> wrote:
> 
>>> Methods that need to be called when they are overridden are
>>> almost always a result of poor design.
>> Without an explanation or data to back this statement, it's hard to argue about it, and I won't make random shots. 
>> 
>> But as a matter of fact, we have to deal with methods which require the behaviour in question now, and I have no idea when Cocoa will be replaced with something better.
> 
> That's a great point.  I just don't want to punish those who create methods with default implementations that are designed to be completely replaced in subclasses.  I suppose that *could* encourage more use of protocols, but that seems like a very unfriendly way to reach a protocol-oriented utopia  ;-)
> 
>> Just out of sheer curiosity:
>> What would be a better design for a method like viewWillAppear? The obvious alternative that is possible with current Swift and template methods looks really ugly to me:
>> Not only would you have to add two empty methods to each subclass of UIViewController,
> 
> Why two, and why empty?  UIViewController would supply its own empty default implementation.
> 
>> but also rely on that those methods are only overridden once in the hierarchy of inheritance.
> 
> There's no law saying you *can't* call super if you absolutely must build a two-level hierarchy below UIViewController (though there are ways around that—the main one being to avoid inheritance hierarchies).  I just don't want to create a world where you're always expected to call super everywhere, which is what would happen if we always warn when you don't call super.
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20161129/7517746a/attachment.html>


More information about the swift-evolution mailing list