WARN on implicit super?

Chris Katko ckatko at gmail.com
Thu Dec 21 06:36:09 UTC 2017


Is there any way to get a warning anytime an implicit super 
constructor is called in a sub-class/child-class?

I have game objects with defaults. I specialize them with 
specifics. The problem is, if I forget to add an explicit super 
call and have it _before_ my code, my code runs, then the super 
constructor code unsets my values.

Now that I know why it's doing it, I can look for it, but Good 
Code (TM) would definitely benefit from an automatic warning / 
check for such cases. Opt-out, not opt-in, for error checking.

Example code:

class object_t
  {
  bitmap_t bmp;
  float x,y;
  this(float _x, float _y)
     {
     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.

     bmp = BMP_BUILDING;
     }
}

Thanks.

At first glance, I could make the default constructor this() with 
an assert(0) if I never need the default constructor and that's 
the one automatically called. But I'd rather have a warning 
instead of opt-in coding.

The auto-calling really seems to be a dangerous mis-feature.


More information about the Digitalmars-d-learn mailing list