-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support final methods #33446
Comments
Just in case tl;dr, Honestly, a few years ago, I would have agreed with you. But I simply don't write much OOP code anymore. The In case it wasn't clear, I mean both |
Stats for If someone can provide the stats for Interestingly enough, searching Github shows that PHP still has a really high final-to-abstract ratio (higher than I expected) but it matches my personal experience that C++ matches my expectations closer. But I suspect it is because it's harder to search for the I'm surprised JavaScript got results at all for Scala is treated differently because the syntax is
What's also interesting (to me) is that JS doesn't have concepts like abstract and final, natively, but some people still seek these concepts out. However, the numbers are tiny for JS. For both abstract and final. But https://github.com/search?q=%22abstract+class%22+in%3Afile&type=Code
https://github.com/search?q=%22final+class%22+in%3Afile&type=Code
https://github.com/search?q=%22abstract+final+class%22+in%3Afile&type=Code
https://github.com/search?q=%22sealed+class%22+in%3Afile&type=Code
https://github.com/search?q=%22abstract+sealed+class%22+in%3Afile&type=Code
https://github.com/search?q=%22sealed+abstract+class%22+in%3Afile&type=Code
|
Right, most people would not code a final method. The main use case is for authors of base classes to prevent sublasses from accidentally overriding base class methods that are critical to proper operation. Thus by definition most people will not author a final method, but they will definitely be protected with subclassing toolkit base classes from accidentally blowing the framework invariants. So sure, I'm glad you don't have to worry about that. And maybe you never use subclasses at all. OK, So why on earth does it bother you that other people do use subclasses? Any why does it bother you that some base class authors need final to avoid accidents? |
Before this flames out tediously, can we please address the main point that Ryan raised (#8306 (comment)):
Is there any citable evidence that this is progressing in the ECMAScript spec. If not, the discussion should really be taken elsewhere. |
Does that comment address final METHODS? Perhaps it does, I'm having trouble parsing it. |
Because it takes time away from other features I want the TS team to work on =P
Much of what Ryan said about Honestly, though, I'm very interested in the stats, regarding final-vs-abstract-vs-plain methods. |
Ignored !== didn't go the way you wanted them to. We've considered both final classes and final methods and still consider them to be something we don't need to add to the language ahead of TC39. |
Two distinct questions:
|
Now you might view Similar to |
I rarely need the final method, but this morning it would have prevented a coworker of mine to introduce a bad side effect. export abstract class Base<T, U= any> {
constructor(protected data: T ) {}
// I wished this to have been final
public getData(): T {
return this.data;
}
}
export class SubClass extends Base<AnInterface> {
// to prevent this side effect
public getData(): (AnInterface & AnotherInterface) {
return { ...super.getData(), otherField: {} };
}
// I was wondering why no data were empty
public isEmpty(): boolean {
return !this.getData();
}
} It crashed during the runtime which is in my opinion, too late. |
Here is my understanding of this topic: final methods are desirable for some toolkit designers, but pose problems for the language implementers. Ryan - I'm NOT trying to re-open this debate (nor to get more educated at this time) just trying to frame and summarize the issue for any future readers. I do hope that someday there will be the time and energy to support final methods in JS/TS, but I understand it will not be any time soon. |
Workaround we use on our team, seems to do the job: class Foo {
protected readonly libraryMethod = () => {
// do library things here
};
}
class Bar extends Foo {
protected libraryMethod = () => {...} // compile error
protected libraryMethod() {...} // compile error
protected doSomethingElse() {
this.libraryMethod(); // this is fine
}
} |
This workaround has an unfortunate runtime performance cost, since it instantiates a new closure on every constructor invocation. If you have a large number of final methods on an otherwise cheap class that's instantiated many times, this can be a non-starter. It can be generalized, though: class Foo {
readonly libraryMethod: () => void;
}
(Foo.prototype as any).libraryMethod = function() {
// now this is cheap again
}; It's not great for readability, but it's more performant for cases where that matters. |
The Dart programming language's solution, fwiw, is a As of Dart 3.0 (2023), |
This ticket is intended to be an open ticket referring to the many long-standing (and long ignored) requests for final methods. These requests have been made in two related, but closed tickets, namely
Sadly, both of these tickets have been closed, due solely to arguments by the developers against final classes. The completely distinct requests for final methods were totally ignored (even though they now comprise the bulk of the comments).
So hopefully the topic of final methods can be addressed on its own merits.
The text was updated successfully, but these errors were encountered: