Swift should provide a way to indicate when overriding a particular method must call super at some point in the implementation and give a compile error whenever this is not done

For example, when overriding viewDidAppear, you need to call super. If you don't, then you should get a compile error.

Swift could also provide a way to indicate when calling super is unnecessary and generate a warning whenever you do this.

4 Likes

I can't think of any mechanisms that the replace the "subclass but ensure you call the super version" pattern. But if there aren't any, I would love to see this some kind of static enforcement.

It seems similar enough to the existing definite initialization checks done within initializer bodies, that I think it's probably not to difficult to implement, but that's just my speculation.

Hello,

You are right, this is an interesting design area.

Since this feature is not currently baked in Swift, I use SwiftLint for this exact purpose. It is very easy to integrate into your Xcode projects, and it generates warnings or errors for most UIKit methods that needs a super call: viewDidAppear, awakeFromNib, etc.

Have a look at it one day!

The problem with SwiftLint is almost everything it warns about is not all that important for solo developers. Maybe it should have a "solo developer mode" that focuses on features like this that are important even for solo developers.

I understand what you say. Writing a .swiftlint.yml file that removes undesired rules is a chore. But this is past me, now that I have done this task once: I have a .swiftlint.yml file that I pass along, from projects from projects, and I do no longer really think about it. It occasionally requires a small massage, and that's it.

This has been discussed many times the past. A more recent thread discussing this: Annotation/Keyword To Automatically Call Super in Override Methods

1 Like

That's why it has a Configuration. Just disable any rules you don't care about.

1 Like
Terms of Service

Privacy Policy

Cookie Policy