digitalmars.D.learn - WARN on implicit super?
- Chris Katko (39/39) Dec 20 2017 Is there any way to get a warning anytime an implicit super
- =?UTF-8?Q?Ali_=c3=87ehreli?= (49/51) Dec 20 2017 There can be a number of solutions but can you please demonstrate the
- Chris Katko (5/13) Dec 20 2017 Well, crap, I'm writing this post a few days after I encountered
- bauss (33/86) Dec 21 2017 This is what I would believe __IS__ and __SHOULD__ be the default
- Chris Katko (10/45) Dec 23 2017 I think during many late night debugging, I had TWO bugs and
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.
Dec 20 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
Dec 20 2017
On Thursday, 21 December 2017 at 06:47:25 UTC, Ali Çehreli wrote:On 12/20/2017 10:36 PM, Chris Katko wrote:Well, crap, I'm writing this post a few days after I encountered the problem and my notes say "super gets called after". I'll have to test what I was thinking and get right back to you tomorrow. Thank you for the prompt reply.[...]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) [...]
Dec 20 2017
On Thursday, 21 December 2017 at 06:47:25 UTC, Ali Çehreli wrote:On 12/20/2017 10:36 PM, Chris Katko wrote: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.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
Dec 21 2017
On Thursday, 21 December 2017 at 10:13:47 UTC, bauss wrote:On Thursday, 21 December 2017 at 06:47:25 UTC, Ali Çehreli wrote:I think during many late night debugging, I had TWO bugs and confused super call-order and wrote that comment. What I think happened was, super() was called correctly, but, the chain of classes was calling this()/super() constructors and DIDN'T call this(var, var)/super(var,var) constructors that I needed. So AFAIK, I'd call this a closed problem / mistaken understanding. Thanks for your prompt replies and help. Sorry if I wasted your time.[...]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.
Dec 23 2017