<div dir="ltr">Matthew,<div><br></div><div>This does not address the issue that a class may require all its subclasses&#39; initializers to call one of its initializers, but does not care which one.</div><div><br></div><div>Are there other languages worth investigating which have the requires super first/last semantics you note?</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Jan 18, 2016 at 10:19 AM, Matthew Johnson via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class=""><br>
&gt; On Jan 17, 2016, at 5:54 PM, Jesse Squires via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br>
&gt;<br>
&gt; Hey all — this is my first reply to this list, so please forgive me if something goes wrong.<br>
&gt;<br>
&gt; I wanted to elaborate more on Nate Birkholz&#39;s earlier message about extending the &#39;required&#39; keyword to class methods, other than only init.<br>
&gt;<br>
&gt; I had suggested this to Joe Groff on Twitter, and encouraged Nate to go ahead and start the thread. I’m just now finding the time to respond :) My goal here is to flesh out this idea a bit more, and hopefully get a more engaging discussion started.<br>
&gt;<br>
&gt; Subclassing in Objective-C has the following deficiencies:<br>
&gt;<br>
&gt; (1) It lacks the ability to formally declare if a method must be overridden.<br>
&gt;<br>
&gt; (2) When overriding a method, there is no mechanism to enforce a call to super. You could use NS_REQUIRES_SUPER, but not only is this barely discoverable, it isn’t part of the language. Further, it merely generates a warning when library authors most likely intend for this to be an error. (Ok, one could treat warnings as errors, etc. etc.)<br>
&gt;<br>
&gt; (3) It is easy for clients to override a superclass method without knowing it. That is, subclasses can simply implement an identical selector of the superclass without any kind of warning.<br>
&gt;<br>
&gt; Clearly this is problematic. What happens when a subclass does not call super, but should? I think the only answer is &quot;undefined behavior&quot;. Typically, the (not so great) solution here is documentation. This is common in Cocoa and CocoaTouch, for example each time you (never) read the docs, you&#39;ll see things like &quot;your implementation must call super at some point&quot;.<br>
&gt;<br>
&gt; Swift improves on these deficiencies in the following ways:<br>
&gt;<br>
&gt; (1) For public methods (or internal for classes in the same module), Swift requires the &#39;override&#39; keyword. This is great. Clients are now aware that they are overriding a superclass method. Though this *should* be an indication to the client that super might need to be called and the docs should be read (lol), clients are not required to do either.<br>
&gt;<br>
&gt; (2) Superclasses can prevent overriding using the &#39;final&#39; keyword. This provides a lot of safety but results in an &quot;all or nothing&quot; decision for library authors.<br>
&gt;<br>
&gt; (3) For initializers only, Swift enforces the &#39;override&#39; keyword *and* a call to super due to Swift’s strict initialization rules. Thus, client subclasses can safely override initializers. Woo!<br>
&gt;<br>
&gt; (4) Additionally, for initializers only, Swift superclasses can require that subclasses implement a specific initializer using the &#39;required&#39; keyword (given that a subclass provides a custom init). Again, augmenting the subclass initialization flow can be done safely. ::applause::<br>
&gt;<br>
&gt; OK. Hopefully I didn’t miss anything there. As you can see, there’s one major deficiency remaining in Swift:<br>
&gt;<br>
&gt; - If a subclass chooses to override a non-final method of its superclass, there is no mechanism to require a call to super.<br>
&gt;<br>
&gt; My proposed solution is straight-forward: extend the use of &#39;required&#39; to methods, rather than only allow this for init.<br>
&gt;<br>
&gt; Behavior would be the following:<br>
&gt;<br>
&gt; - A superclass can specify a method as &#39;required&#39;.<br>
&gt; - *If* a client’s subclass chooses to override a &#39;required&#39; method, it must call super.<br>
&gt; - Without the &#39;required&#39; keyword, current behavior would remain the same.<br>
<br>
</span>Thanks for bringing up this topic.  I agree that we need to support more expressive semantics for overrides.  I would take a slightly different approach though.<br>
<br>
Your use of `required` has a different semantic than its use for initializers.  The change is arguably subtly, but it is meaningful.  I don’t think it is a good idea for it to have different semantics in different contexts.<br>
<br>
For initializers `required` means “subclasses must provide an initializer with this signature.  The fact that you have to call super just falls out of that because it is an initializer.  You don’t actually have to call designated initializer that has the matching signature.<br>
<br>
You are suggesting making `required` mean “you must call the exact same method on super”.  This would be unnecessarily limiting.<br>
<br>
There are 3 possible semantics for overridability:<br>
<br>
1) final / not overridable<br>
2) overridable<br>
3) required<br>
<br>
I suggest expanding the use of `required` to mean #3 in general.  I would also suggest changing the default to #1.  That would require developers to think about subclasses before allowing overrides and also decide whether #2 or #3 is the correct semantics when overriding is allowed (I know this is controversial and somewhat orthogonal to your idea).  This would prompt the developer to think about the requirements to call super, which I discuss next.<br>
<br>
There are also 4 possible semantics for &quot;super requirements” when you do override:<br>
<br>
1) no call to super required<br>
2) requires super (must call super *if overriden*)<br>
3) requires super first<br>
4) requires super last<br>
<br>
NS_REQUIRES_SUPER is equivalent to #2.<br>
<br>
The last two are somewhat subtle, but it is occasionally reasonable for a superclass to want its implementation to run before or after a subclass implementation.  In both of these cases I would advocate for the compiler to automatically synthesize the call to super (if Swift supports these semantics) rather than requiring the developer to write the call.  The annotation specifies the behavior and the manual call would be redundant.<br>
<br>
Swift doesn’t currently have any rules around calling super so we have #1 as a de-facto default with no way to change that.  That is probably ok as a default, especially if we require developers to think about subclasses when writing an overridable method (by making final the default).  If we introduce syntax to specify 2-4 we would have all cases covered.<br>
<br>
Any of the “super requirements” could be specified in conjunction with either `overridable` or `required`.  This allows more expressiveness than mixing the two concepts together would support.<br>
<br>
At minimum, I suggest that you use `required` as mentioned above and introduce new syntax to mean “must call the super implementation of the same method”.  Adding “super first” and “super last” would be nice as well, but can also be added later if you’re not interested in those semantics.<br>
<br>
Matthew<br>
<div class="HOEnZb"><div class="h5"><br>
&gt;<br>
&gt; Comments from Joe via twitter:<br>
&gt; - &quot;Should be straight forward to implement.&quot;<br>
&gt; - &quot;One interesting thing it would enable is invariant implementations of covariant protocol requirements.&quot; &lt;dogScience.png&gt;<br>
&gt;<br>
&gt; Thanks for reading!<br>
&gt; Jesse<br>
&gt;<br>
&gt; _______________________________________________<br>
&gt; swift-evolution mailing list<br>
&gt; <a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
&gt; <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br>
_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">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>
</div></div></blockquote></div><br></div>