June 08, 2015 Re: Constructor inheritance? Why not? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Ola Fosheim Grøstad | On Monday, 8 June 2015 at 09:20:06 UTC, Ola Fosheim Grøstad wrote: > They can if you use type variables. Type variables can be virtual too. And IIRC you can emulate type variables in D too if you dig into RTTI, but I don't quite remember where I found that. I am sure you know how to do it. IIRC it was not difficult. You might want to look into that and polymorphic construction as a library feature, it allows for dynamic generics. Not for every day use, but allows some advanced modelling. > but you had "inner" inheritance opposed to Simula/C++ "outer" inheritance. Actually Simula has "inner" inheritance for the constructor and not for the virtual functions, when I think of it, wheras Beta is more consistent and have "inner" semantics on both. C++ just messed that up in the translation. | |||
June 08, 2015 Re: Constructor inheritance? Why not? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Ola Fosheim Grøstad | On Monday, 8 June 2015 at 09:20:06 UTC, Ola Fosheim Grøstad wrote:
> 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.
>
> They can if you use type variables. Type variables can be virtual too.
>
>> 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.
>
> Conceptually you should. In Simula's successor Beta you had to, but you had "inner" inheritance opposed to Simula/C++ "outer" inheritance. E.g. the logic of the subclass is executed by the superclass. It is much cleaner, actually.
>
>> 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.
>
> Let's not rewrite history, shall we? Constructors are not fundamentally different from other functions by nature. In minimal OO languages there is no significant difference between the definition of a class/constructor and a function.
Yep, in Modula-3 and Oberon, one even has to create the VMT explicitly.
--
Paulo
| |||
June 08, 2015 Re: Constructor inheritance? Why not? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Tofu Ninja | On Monday, 8 June 2015 at 02:39:22 UTC, Tofu Ninja wrote:
> Is there any reason why constructors are not inherited? All other methods are inherited, why not constructors?
It should be relatively easy to do this with a mixin in theory. Or even a CtorArgsTuple, allowing something like (not certain if this would work):
this(CtorArgsTuple!super args, bool animated) {
super(args);
this.animated = animated;
}
Also not sure if the '!super' would work as opposed to specifying the type.
This approach has the issue of not inheriting documentation though, which annoys me.
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply