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

Make call to super() implicit within constructor #5910

Closed
nippur72 opened this issue Dec 3, 2015 · 11 comments
Closed

Make call to super() implicit within constructor #5910

nippur72 opened this issue Dec 3, 2015 · 11 comments
Labels
Declined The issue was declined as something which matches the TypeScript vision Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript

Comments

@nippur72
Copy link

nippur72 commented Dec 3, 2015

Calling super() within a constructor is a bit redundant and annoying... can we have it called implicitly if call to super is omitted?

For example this

class Manager extends Employee {
   constructor() {
      this.title = "The Manager";
   }
}

should be equivalent to

class Manager extends Employee {
   constructor() {
      super(); // call to default constructor added implicitly      
      this.title = "The Manager";
   }
}

(C# also does a similar thing and it's a nice feature).

@coreh
Copy link

coreh commented Dec 3, 2015

What would be the default behavior for the parameters? Would they be passed on to the super class constructor if the signature was the same?

@nippur72
Copy link
Author

nippur72 commented Dec 3, 2015

@coreh that would be a possibility, since you can always override it by making an explicit call to super(). It would do no harm to existing code.

@GoToLoop
Copy link

GoToLoop commented Dec 3, 2015

Gonna add that like C#, Java does the same too for empty super constructors! :D

@mhegazy
Copy link
Contributor

mhegazy commented Dec 3, 2015

TypeScript is and should be a super set of JavaScript ES6 semantics has no implicit call for constructors, if TS supports that, a TS/ES6 class are not identical in behavior.

@GoToLoop
Copy link

GoToLoop commented Dec 3, 2015

As long as some handy feature isn't detrimental to ES6 compatibilty, I don't see why it should be thrown away.
This proposal merely asks for TS' compiler to match some of the smartness of C# & Java compilers.
It won't affect codes which don't use this saving grace feature.

Since TS is a compiler/transpiler it can do tricks that raw JS can't after all.
Many C# & Java programmers would be attracted to JS world if TS would mirror many of C#/Java features where it's feasible to do so.

@nippur72
Copy link
Author

nippur72 commented Dec 3, 2015

peraphs there could be a compiler flag like #274 to turn on and off features like this one.

@ghost
Copy link

ghost commented Dec 4, 2015

What if my intention was to call super() after the this.title assignment in certain use-case.
We cannot generalize when the user wants to call super() ctor, therefore this seems like bit over-engineering TBH.

@nippur72
Copy link
Author

nippur72 commented Dec 4, 2015

@jasonwilliams200OK the new behaviour would be:

  • if there's no super() call and the class has a default constructor, instead of throwing an error, make a call to super() as the first thing in the constructor.
  • else work as before

Your special case would be covered by calling super explicitly. (And btw, is it possible to refer to this before super?)

@RyanCavanaugh
Copy link
Member

What if my intention was to call super() after the this.title assignment in certain use-case.

Note that this is disallowed in ES6. You cannot access this until after the super call.

@ghost
Copy link

ghost commented Dec 5, 2015

@RyanCavanaugh, thanks, this. was indeed a bad example. The point was to retain user in control to defer super() call as first thing in the ctor.

@mhegazy mhegazy closed this as completed Feb 19, 2016
@mhegazy mhegazy added Suggestion An idea for TypeScript Declined The issue was declined as something which matches the TypeScript vision Out of Scope This idea sits outside of the TypeScript language design constraints labels Feb 19, 2016
@mhegazy
Copy link
Contributor

mhegazy commented Feb 19, 2016

As mentioned above, we would like to keep the semantics matching JS, and issue warnings to help guide the user the correct behavior.

@microsoft microsoft locked and limited conversation to collaborators Jul 31, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Declined The issue was declined as something which matches the TypeScript vision Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants