Constructor inheritance? Why not?
Jonathan M Davis via Digitalmars-d
digitalmars-d at puremagic.com
Sun Jun 7 21:32:40 PDT 2015
On Monday, 8 June 2015 at 04:16:14 UTC, Tofu Ninja wrote:
> Their are plenty of examples where you would want a constructor
> to be inherited, exceptions being a good one. Currently if you
> have a base class with a constructor that you want all the sub
> classes to have as well, you simply have to just copy past them
> all over the place.
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.
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.
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.
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.
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.
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.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list