Re: WARN on implicit super?

2017-12-23 Thread Chris Katko via Digitalmars-d-learn

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:

[...]


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.


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.


Re: WARN on implicit super?

2017-12-21 Thread bauss via Digitalmars-d-learn

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.


Re: WARN on implicit super?

2017-12-20 Thread Chris Katko via Digitalmars-d-learn

On Thursday, 21 December 2017 at 06:47:25 UTC, Ali Çehreli wrote:

On 12/20/2017 10:36 PM, Chris Katko wrote:

[...]


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)


[...]


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.


Re: WARN on implicit super?

2017-12-20 Thread Ali Çehreli via Digitalmars-d-learn

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


WARN on implicit super?

2017-12-20 Thread Chris Katko via Digitalmars-d-learn
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.