question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

keyword to force calling the super on any method

See original GitHub issue

Today 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:open
  • Created 6 years ago
  • Reactions:89
  • Comments:49 (4 by maintainers)

github_iconTop GitHub Comments

27reactions
briancodescommented, Apr 29, 2018

@Xample I had the same problem with trying to ensure that classes that extended a BaseEventService class called super.ngOnDestroy if they override the ngOnDestroy 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 implemented

One way to do this, which works if the sub classes call ngOnDestroy, ngOnDestroy(){ super.ngOnDestroy()}, or if they don’t implement ngOnDestroy at all

In the Base Class

    constructor() {
        super();
        let originalOnDestroy = this.ngOnDestroy;
        this.ngOnDestroy = () => {
            this.destroy();
            originalOnDestroy.apply(this);
        };
    }

    ngOnDestroy(): void {
        // do nothing
    }

    private destroy() {
        // clears all subscriptions etc
    }
23reactions
Xamplecommented, Mar 8, 2018

@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:

export class BaseComponent implements OnDestroy {
  ngOnDestroy() { 
 // clean up stuff
 }
}

where OnDestroy is as follow:

export interface OnDestroy {
    ngOnDestroy(): void;
}

now imagine I would extend this class

export class AClass extends BaseComponent {
  ngOnDestroy() { 
 // problem if I do not call the super ngOnDestroy() function
 }
}

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)

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found