keyword to force calling the super on any method
See original GitHub issueToday I faced the following code
class A {
// silly warning comment: if you override this, don't forget to call the super method to avoid memory leaks
onExit() {
// do some important cleaning stuff
}
}
class B extends A {
onExit() {
super.onExit(); // good
}
}
class C extends A {
onExit() {
// forgot to call to super.onExit = memory leaks
}
}
The problem is that, unlike a constructor, there is no way to force a method overriding another one to call the parent “super” function.
I wished we had a “concrete”* keyword to let the user know he must call the super method.
class A {
concrete onExit() {
// do some cleaning stuff
}
}
class B extends A {
onExit() {
super.onExit(); // no error
}
}
class C extends A {
// error: Declaration of derived method must contain a 'super' call
onExit() {
}
}
In another language, I could have used the final
keyword to prevent overriding the method but then… no overriding allowed neither.
- “concrete” In opposition to “abstract” (for lack of a better name), other ideas: “important” or “mandatory”
Issue Analytics
- State:
- Created 6 years ago
- Reactions:89
- Comments:49 (4 by maintainers)
Top Results From Across the Web
java - How to force derived class to call super method? ...
The only place where you can force calling of super is in constructors. All constructors have to call a superclass constructor. One (the...
Read more >9.4. super Keyword — AP CSAwesome
The keyword super is very useful in allowing us to first execute the superclass method and then add on to it in the...
Read more >super - JavaScript - MDN Web Docs - Mozilla
The super keyword is used to access properties on an object literal or class's [[Prototype]], or invoke a superclass's constructor.
Read more >How to Emulate The "super" Keyword In C++
A derived class sometimes needs to call the code of its base class and name it explicitly. But for bases classes with a...
Read more >Supercharge Your Classes With Python super()
super () alone returns a temporary object of the superclass that then allows you to call that superclass's methods. Why would you want...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@Xample I had the same problem with trying to ensure that classes that extended a BaseEventService class called
super.ngOnDestroy
if they override thengOnDestroy
in their implementations. I couldn’t find a way to enforce it with warnings/TypeScript constraints, but I can ensure that the functions in the Base Class get called regardless of how the sub classes are implementedOne way to do this, which works if the sub classes call
ngOnDestroy
,ngOnDestroy(){ super.ngOnDestroy()}
, or if they don’t implementngOnDestroy
at allIn the Base Class
@dardino it’s a solution if it’s your own code and you do not rely on interfaces. “onExit” being part of an interface it cannot be private. The real life use of my proposal is mostly for hooks. Like we see in angular or [ionic] (https://ionicframework.com/docs/nightly/api/navigation/NavController/#lifecycle-events) Lifecycle events (but not restricted to).
let’s take the following example:
where OnDestroy is as follow:
now imagine I would extend this class
A “mandatory” keyword would print an error if we forget to call the super method. (as it is already the case with constructors of class extending another one)