WARN on implicit super?

bauss jj_1337 at live.dk
Thu Dec 21 10:13:47 UTC 2017


On Thursday, 21 December 2017 at 06:47:25 UTC, Ali Çehreli wrote:
> On 12/20/2017 10:36 PM, Chris Katko wrote:
>> Is there any way to get a warning anytime an implicit super 
>> constructor is called in a sub-class/child-class?
>
> There can be a number of solutions but can you please 
> demonstrate the issue with compilable code? My attempt does not 
> agree with your description: super() is called *before* the 
> subclass constructor. (Compiled with DMD64 D Compiler 
> v2.077.1-384-gc6829a8)
>
> import std.stdio;
>
> // To get the code compile:
> enum : int {
>     BMP_PLACEHOLDER,
>     BMP_BUILDING
> }
> alias bitmap_t = int;
>
> class object_t
> {
>     bitmap_t bmp;
>     float x,y;
>     this() {
>         writeln("super()");
>         bmp = BMP_PLACEHOLDER;
>     }
>
>     this(float _x, float _y)
>     {
>         writeln("super(float, float)");
>         bmp = BMP_PLACEHOLDER;
>         x = _x;
>         y = _y;
>     }
> }
>
> class building_t : object_t
> {
>     this(float _x, float _y)
>     {
>         //        super(_x, _y);
>         // ^^ If I forget this, it implicitly gets called AFTER
>         // this function is done. Which resets bmp to 
> BMP_PLACEHOLDER!
>         // AND, it'll call the DEFAULT constructor this() with 
> no arguments.
>
>         writeln("setting in building_t");
>         bmp = BMP_BUILDING;
>     }
> }
>
> void main() {
>     auto b = new building_t(10, 20);
>     assert(b.bmp != BMP_PLACEHOLDER);
> }
>
> Prints
>
> super()
> setting in building_t
>
> Ali

This is what I would believe __IS__ and __SHOULD__ be the default 
behavior too, because that's how it generally is in other 
languages.

It doesn't make much sense to call a super constructer after and 
it's very rare cases that you need too.

The only time you really call super constructors after is if the 
parameters are different.

Ex.

class Foo
{
     int baz;
     int boo;

     this() { ... }

     this(int baz, int boo) { ... }
}

class Bar : Foo
{
     this()
     {
         int baz =getBazFromSomewhere();
         int boo = getBooFromSomewhere();
         super(baz, boo);
     }
}

In that case it makes sense to call super() after, but you rarely 
end up in cases like that and thus you should generally be able 
to omit the call.

If super() is ever called explicit after (if no call to a super 
constructor has been done.) then I can only imagine A LOT of code 
will break, because it's a general concept and known behavior 
from most languages that base/super constructors are called 
before.


More information about the Digitalmars-d-learn mailing list