Constructor inheritance? Why not?

Tofu Ninja via Digitalmars-d digitalmars-d at puremagic.com
Sun Jun 7 23:04:21 PDT 2015


On Monday, 8 June 2015 at 04:32:41 UTC, Jonathan M Davis wrote:

> In most cases, you need to create a new constructor, because 
> it's a different type with different data, even if some of the 
> constructor parameters might be the same. Exceptions are a rare 
> case where the constructors are frequently the same, because 
> frequently with exceptions all you care about is the type 
> itself, not additional data. But that's far from normal.

I am not sure why you think its a rare case, I hit it all the 
time. Something where I don't want to change much in the sub 
class other then maybe override a method or something.


> Certainly, if there's any kind of constructor inheritance, I 
> think that it needs to be explicit, because otherwise, you'll 
> accidentally end up with constructors that you don't even know 
> you have.

I fail to see how that would be a problem, you have the same 
thing with normal methods.

> But while it might be nice to avoid boilerplate in exception 
> code, I question that it's worth much in the general case, 
> because it's only useful when you don't have additional data 
> that needs to be passed to the constructor of a derived class.

A lot of the time when I add new data to a class, I will just 
statically initialize it, in those cases this would still be 
useful.

> Regardless, we're talking about syntactic sugar here. Maybe 
> it's worth adding. Maybe not. But the primary reasons that we 
> don't have it are because the languages before us didn't it 
> (meaning that we're less likely to have thought of it), and 
> because it's not actually needed, so we can get by fine without 
> it.

Would save time and reduce copy past errors, I see value in that.

> But the type of "inheritance" that you're talking about with 
> constructors is fundamentally different from the type of 
> inheritance that you get with other functions, so I don't think 
> that the fact that we have class inheritance makes it at all 
> obvious that we would have constructor inheritance or even that 
> we should have it.

I don't really see how its any different, define a constructor in 
a base. Gets inherited, if you want it to do something different 
then you override it, have the option to call the base version 
like normal overriding does(only difference is that at least some 
version of the base needs to be called which is actually a 
feature that other methods could benefit from having). The only 
use that would be slightly different is it would be harder to get 
polymorphism out of it(still possible if you you want to allocate 
and deffer construction to a later time).

> Really, it's just a question of whether this particular bit of 
> syntactic sugar is worth adding (which is debatable). It's not 
> something fundamental about how class inheritance and 
> polymorphism work.
>
> I expect that if someone came up with a good DIP for this and 
> implemented a PR for it that they'd stand a reasonable chance 
> of getting it into the language, but it's not exactly something 
> that folks have been screaming for.

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.


More information about the Digitalmars-d mailing list