Skip to content
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

Suggestion: Final keyword for classes and methods #9264

Closed
0815fox opened this issue Jun 20, 2016 · 9 comments
Closed

Suggestion: Final keyword for classes and methods #9264

0815fox opened this issue Jun 20, 2016 · 9 comments
Labels
Duplicate An existing issue was already created

Comments

@0815fox
Copy link

0815fox commented Jun 20, 2016

I'd like to have a final keyword on classes and methods, which specifies, that the class / method must not be overridden by any typescript class. Compile-time checks should be done, such that the following examples generate a tsc compiler error.

final class Foo {

}

class Bar extends Foo {

}
// => Bar cannot extend Foo, because it is final.
class Foo {
  final fooIt():void{

  }
}

class Bar {
  fooIt():void {

  }
}
// => Method fooIt of Bar cannot override fooIt of Foo, because it is final.

E.g. I often use following pattern, where I want to urgently avoid fooIt to be overridden:

import Whatever ...

abstract class Foo {
  private ImportantVariable:boolean;

  protected abstract fooIt_inner:Whatever();

  public final fooIt():Whatever() {
    //do somestate change to aprivate member here, which is very crucial for the functionality of every Foo:
    ImportantVariable = true;
    //call the abstract inner functionality:
    return this.fooIt_inner();    
  }
}
@kitsonk
Copy link
Contributor

kitsonk commented Jun 20, 2016

Dupe of #8306 which was closed as "won't fix" for the reasons listed in that issue.

@0815fox
Copy link
Author

0815fox commented Jun 20, 2016

Only half of it. How would you provide a final keyword for methods?

Actually, I often see argumentations like those in #8306 in this issue tracker, which state, that something would not add any value, like mhegazy in the last comment of that closed issue. That's really a pitty, because it does add value. It prevents users from accidentially doing something unintended, when they want to extend functionality. Of course you can write a comment for that, but that is error prone. I can also write a comment that states, that a user should only pass in string for my argument x of my function. So why should I use Typescript then? I use it, because it adds value. It prevents me from making stupid mistakes in a large project with many involved parties and I am very happy to have it. So: why did they introduce the abstract keyword? what can they improve with this? Users could also write a comment which states, that the class should not be instanciated. And one could also avoid exposing it as class, like the argument in #8306.

@mhegazy
Copy link
Contributor

mhegazy commented Jun 20, 2016

This is indeed a duplicate of #8306. We would appreciate it if you can keep the discussion in one thread; it makes it easy to track all proposals, and limits noise. we do not lock closed issues, so feel free to comment on them as well.

@mhegazy mhegazy added the Duplicate An existing issue was already created label Jun 20, 2016
@0815fox
Copy link
Author

0815fox commented Jun 20, 2016

Then you should add the final keyword for methods topic to the title as well. Or shall I open another issue for the final statement on methods, which is not handled by #8306 at all?

@mhegazy
Copy link
Contributor

mhegazy commented Jun 20, 2016

I do think they are more or less the same issue to be frank.

@aluanhaddad
Copy link
Contributor

aluanhaddad commented Jun 21, 2016

So: why did they introduce the abstract keyword?

@0815fox it's worth noting that the abstract keyword affects code generation in a meaningful way.

Consider abstract-thing.ts

abstract class AbstractThing {
    abstract doSomething(): void;
}

abstract-thing.js

var AbstractThing = (function () {
    function AbstractThing() {
    }
    return AbstractThing;
}());

I'm not sure final methods could be emitted which would give a false sense of security. Now this is just my viewpoint. I avoid the private keyword for largely the same reason.

@0815fox
Copy link
Author

0815fox commented Jun 21, 2016

I agree with you, there will probably be no mechanism which prevents final methods from being overridden in JavaScript. But overriding them from within typescript should be made impossible, unless you are doing crazy casts or use any.

For me, TypeScript is something like a safety belt. It will not protect someone, who drives without using it, but it should not be removed from cars, just because there are people doing this. Instead it adds security for those using it and does not affect those not using it.

But there is one important thing, which @mhegazy pointed out: They have a limited amount of time and try to use it wise on the most important features. I agree with him, that the final keyword is not the most important feature at the moment. I disagree with him, that it does not add value to the code.

It's a pitty, that they decided to close such issues, because it makes it harder to find them (search by default only shows open tickets). A dedicated label 'got it, but not now' might be more helpful. Then they could lock closed issues for new discussion, such that (partial) duplicates like this one are not commented to anymore.

@mhegazy: Can you somehow close and lock this ticket, such that everyone comments in the duplicate?

@aluanhaddad
Copy link
Contributor

aluanhaddad commented Jun 21, 2016

@0815fox I absolutely agree with making a distinction between closed vs pending issues. It would be nice if GitHub allowed a repository to configure the default search parameters used for searching issues. Even the term issue itself is not particularly descriptive in the sense that it has the wrong connotation for many things which need to be filed as issues. But these are GitHub issues and are far outweighed by the benefits of the overall platform.

@mhegazy
Copy link
Contributor

mhegazy commented Jun 21, 2016

closing in favor of f #8306.

@mhegazy mhegazy closed this as completed Jun 21, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants