Redundant super() calls

Topics: General
Mar 29, 2014 at 1:20 AM
Edited Mar 30, 2014 at 5:36 PM
class Unique {
    uid = Unique['_static_counter']++;
}
Unique['_static_counter'] = 0;
class Kabucha extends Unique {
    constructor(public name:string="busya") { 
        super(); /// <<THIS
    }
}
Link to the code

Why do we need a redundant super() call, when the compiler already knows that we should call it anyway, since a CT-error: "Constructor for derived classes must contain a super() call." ?
Why don't we assume that if there are no explicit super() call, then we always want to place it as a first instruction of a child constructor?
Developer
Mar 31, 2014 at 9:25 PM
What would you inject if the base class constructor has some number of required parameters of specific types?
class Unique {
    uid = Unique['_static_counter']++;
    constructor(name: string) { }
}

Unique['_static_counter'] = 0;

class Kabucha extends Unique {
    constructor(public name: string) {
        // super(null); // inject this? or ""? or name or what?
    }
}
In addition, even if we had an acceptable pattern for this case I would find it a little strange if whenever looking at a derived constructor definition without a super() call I had to manually inspect the conditions surrounding it that may or may not lead to an injected super() call (and then remember what argument values are injected). Sometimes there is value in requiring something to be explicit and consistent.
Apr 1, 2014 at 12:30 AM
Not sure I follow, but in C# (and other languages I'm sure), if the base type has no parameters, the derived type doesn't need to explicitly show code to call it. I don't think there's a good reason to force people to have to add in a "super()" call in this case. In fact, when I started using TS, it was my very first pet peeve.