WARN on implicit super?

Ali Çehreli acehreli at yahoo.com
Thu Dec 21 06:47:25 UTC 2017


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


More information about the Digitalmars-d-learn mailing list