Constructor inheritance? Why not?

Tofu Ninja via Digitalmars-d digitalmars-d at puremagic.com
Mon Jun 8 00:31:59 PDT 2015


On Monday, 8 June 2015 at 06:35:33 UTC, Jonathan M Davis wrote:
> On Monday, 8 June 2015 at 06:04:23 UTC, Tofu Ninja wrote:
>> No its definitely not something that alot of people seem to 
>> care about, but I always found it odd that constructors get 
>> certain special casing like this.
>
> I expect that it originally comes down to the fact that 
> constructors aren't polymorphic and _can't_ be polymorphic. 
> When you override a function, you're overriding the one in the 
> base class and replacing its implementation. You can choose to 
> call the base class' version of the function, but you don't 
> have to. You're dynamically replacing a function.
>
> Constructors, on the other hand, do _not_ override. A 
> constructor must exist in _every_ class in the inheritance 
> tree, and they all have to be called - and called in the right 
> order. When you "inherit" a constructor, you're not overriding 
> the one in the base class. You _still_ have to have 
> constructors in both the base class and derived class. You're 
> just telling the compiler to insert a constructor into the 
> derived class which has exactly the same parameters as one in 
> the base class and which simply forwards its arguments to the 
> base class' constructor. There's no polymorphism involved at 
> all. It's simply syntactic sugar to avoid having to type out 
> the whole constructor implementation yourself. There's nothing 
> dynamic about it all. Inherited constructors are truly just 
> syntactic sugar. So, while they may be useful (as syntactic 
> sugar often is), they add _zero_ functionality, unlike virtual 
> functions.
>
> So, yes, constructors _are_ fundamentally different from other 
> functions. And the inheritance that you get with constructors 
> is not at all what you get when you override a function. 
> They're at most superficially similar. And older languages such 
> as C++98 and Java don't have inherited constructors. They're a 
> relatively new idea. So, if anything, it would be surprising if 
> D had them. Constructors are just different beasts from other 
> functions (the same goes for destructors). And while D has lots 
> of new ideas in it, it's not like we have all of the cool, new 
> language ideas out there. Maybe we'll get constructor 
> inheritance someday, but no one has cared enough to add it thus 
> far.
>
> - Jonathan M Davis

I suppose I just see it differently. I see no reason why 
constructors need to be different from normal virtual methods 
other than the small requirement that they need to call their 
base if they override. That requirement is not really something 
specific to constructors either, plenty of other types of 
functions would benefit from that sort of guarantee.

Constructors need not exist in every instance of the tree, if the 
class implementor deemed the base constructor to be sufficient 
then that should be allowed. Plenty of times people will just 
insert empty constructors in sub classes that only forward 
arguments to the base. That forwarding constructor is not needed, 
just call the base constructor in the same way normal virtual 
methods work.

Also constructors certainly can be polymorphic and the 
aforementioned base call guarantee does not make it so that they 
cant. Deferred construction certainly allows for polymorphic 
construction. Aka allocate and set up static 
information(including type information) without calling a 
constructor so that it can be called at a later time. In the time 
between allocation and construction, that instance could be 
assigned to reference of the base type and construction could be 
polymorphic.

The only issue one would have with this is if a base 
constructor's signature was no long sufficient to fully construct 
the object, in which case it would make sense to just override 
and throw an exception the same way you would restrict any method 
signatures usage. That solution though wouldn't really work for D 
at this point which is why it would probably make more sense to 
make it opt-in at the base class level.



More information about the Digitalmars-d mailing list