[swift-evolution] Add code to super methods.

Haravikk swift-evolution at haravikk.me
Sun Nov 20 07:53:40 CST 2016

> On 19 Nov 2016, at 17:08, Tino Heth <2th at gmx.de> wrote:
> I still thing that the naming is one of the hardest parts:
> Imho "@super" just feels wrong — the word has already a meaning, and it should only be used when referring to the parent class (here, it is a restriction/hint for subclasses).
> The best I can think of would be @extend (because it is about extension of the marked method)

I like it because it aligns with what you're actually restricting, but obviously all naming is up for debate.
The use of final for this purpose is okay, but it doesn't really fit the required and optional attributes which I think are also useful to have now or in future.
@extend is a good possibility though.

> I definitely support the idea in general, so I've something to add:
> — There are already cases where super shouldn't be called (never?)

I can't remember if I had that case in the previous discussion or not, but there's certainly an argument for an additional "never" attribute to prevent the use of super at all!

> — Imho before and after only make sense if the call to super is generated automatically when it is omitted: The restriction on the order afaics offers very little (if any) additional safety, and it isn't clear how defer would interact with before.

I'm not sure I follow your meaning on omission; I quite like Sean's suggestion of having an override(before) or override(final) in the sub-class, this way the call is explicitly auto-generated. I think having it be added automatically in the regular override case could be a bit confusing, I think a warning or error is better in that case as it should be visible so you know exactly when it is being called, the other cases don't need it (as they have the extra information on the override).

I forgot all about defer though! They're a bit trickier in the case of @extend(after) or whatever, since deferred statements cannot be executed before the super call. They should be fine in the @extend(before) case though as they can only run after the super method or when it's in the process of throwing without a try. So I suppose it means they will have to produce an error/warning in a regular override if the @extend(after) restriction is present, but should be fine in the override(after) case (implicit super method call after body) assuming the deferred code would execute before the super call in that case; since it will resembled its own scope this should be fine I think? Otherwise you need to make sure you perform the defer within a more limited scope in order to control when it eventually executes.

> Some questions that should imho be addressed in a formal proposal:
> What about parameters and return values? Should it be possible to change those? I think so, but that would make the feature more dangerous than pure monitoring. If it's forbidden to change anything, we loose flexibility, and if it's configurable, complexity is raised.

This is why I think we still need to allow the general override case so we can put the super method wherever we like with an error/warning if breaks any requirement, as developers can add safe code before or after to do any additional processing of parameters/return values. The override(before) and override(after) would just be a shorthand for when you want 

> On 19 Nov 2016, at 17:29, Mustafa Sabur <mustafa.sabur at icloud.com> wrote:
> The most important part is that it should be easy to understand even for beginning developers. Imo the moment someone have to look it up in the docs to understand it, is the moment it has failed to serve its purpose.

I don't think that Sean's override(before/after) should be any more difficult than addinto; in fact it's arguable that addinto doesn't contain enough information as is unclear what's going on as it looks like an entirely new concept, whereas using the override keyword keeps it clear.

> I just don't like the fact that you have to use the override keyword while that's not your intention and you can forget to call super. And that it's not guaranteed that you code will run since its not final. 

Whether or not it's your intention, overriding is what you're doing; I think it's important to keep that clear. I'm not sure what you mean about finality?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20161120/7d15bd39/attachment.html>

More information about the swift-evolution mailing list