[swift-evolution] Proposal: Add @requires_super attribute

Matthew Johnson matthew at anandabits.com
Wed Dec 16 21:10:37 CST 2015


When a superclass method must be called either before or after the override it would be ideal to have the compiler synthesize the call rather than just verify the developer writes it.  The developer should only need to write it when they need control over when that call happens.

willSet and didSet are pretty similar to this synthesis, but rather than synthesizing a call to super the compiler synthesizes the entire setter with the willSet and / or didSet code in the appropriate location(s).


> On Dec 16, 2015, at 8:28 PM, T.J. Usiyan via swift-evolution <swift-evolution at swift.org> wrote:
> 
> one solution is @requires_super(first|last). This cuts out "call me in the middle" but that is a worthwhile trade, in my opinion.
> 
> On Wed, Dec 16, 2015 at 9:22 PM, Javier Soto via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
> I'm not a fan of this feature because it encourages a type of API design that's really hard to ensure that it's used correctly. The biggest problem is the fact that often when a class expects or allows a method to be overriden, and super needs to be called, it expects the call to happen in a particular order: either before of after the implementation from the subclass. 
> 
> So yes, this is missing from Obj-C, yes, this is important because UIKit, but going forward my feeling is that a language without this feature is a language that will force you to think about an alternative way to implement an API without this, and that'll probably be less error-prone. 
> On Wed, Dec 16, 2015 at 3:03 PM Tino Heth via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> Default behavior would require something like "unrequire" which seems odd.
> 
> the keyword for requires_super is so odd, it doesn't even fit in a single word! ;-)
> I think it is much easer to find a word for something that can be replaced completely than for "you have to call the implementation of the parent class"
> 
>> Also this would break a lot of existing code and migration can't be done automatically.
> 
> Two parts:
> What is a lot? I really don't think calling super hurts in many places.
> Why shouldn't it be possible to migrate automatically? It might get complicated for compiled libs, but definitely possible.
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
> -- 
> Javier Soto  
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
> 
> 
>  _______________________________________________
> 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/20151216/680ee6d4/attachment.html>


More information about the swift-evolution mailing list