[swift-evolution] [Discussion] Enforcing Calling Super

Dany St-Amant dsa.mls at icloud.com
Tue Feb 23 20:07:13 CST 2016


> Le 22 févr. 2016 à 19:51, Kyle Sherman via swift-evolution <swift-evolution at swift.org> a écrit :
> 
> ## Detailed design
> 
> A possible implementation of this may look like this:
> 
> ```
> class MyClass {
>     @requiresSuper func foo1() { }
> 
>     @requiresSuper(start) func foo2() { }
> 
>     @requiresSuper(end) func foo3() { }
> }
> ```
> 
> Now, if the developer were to create a subclass and not call the super method, the compiler should display a warning. The warnings that should be displayed should be similar to: 
> Overridden method must call the superclass’s implementation
> Overridden method must call the superclass’s implementation as the first line of the method.
> Overridden method must call the superclass’s implementation as the last line of the method.
> for the cases of `@requiresSuper`, `@requiresSuper(start)`, and `@requiresSuper(end)` respectively.
> 

Been following the many discussions on this topic and I do not think  that these start/end requirements will ever fly. Imposing such a specific location on the sub-classes will cause a lot of troubles, whether it being unable to add debug log, or to tweak parameters before passing them to super, in the end the developer of the main class won’t dare use such specification, to avoid pushing away potential sub-class developer, and as a way to preemptively mute their complaining.

OTOH, my guts are telling me that, some similar form of control may be desired on some occasion mainly when playing with resources, maybe for a setup-like method where super should be call before any other methods provided by the class, and or for close-like method, the reverse.  Unfortunately, unless such rules are made rigid (all or nothing) such syntax would likely be too convoluted and long; super.close() must be call after super.read(), super.write(), ...

A possible syntax for the rigid rules would be:

@requiresSuper(before: methods)
@requiresSuper(after: methods, propertiesSetter)
@requiresSuper(before: methods, propertiesGetter)

But, I do not have any examples in mind to corroborate that such options are better than the proposed @requiresSuper(start) and @requiresSuper(end). IMHO, I just think that @requiresSuper(start) and @requiresSuper(end) should be drop as too intransigent, which will cause them to be rarely used.

Dany


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


More information about the swift-evolution mailing list