View mode: basic / threaded / horizontal-split · Log in · Help
March 04, 2008
Re: constructor inheritance
On Tue, 04 Mar 2008 22:25:15 +0100, Robert Fraser  
<fraserofthenight@gmail.com> wrote:
> How about just:
>
> class Child : Parent
> {
>      this(int x, int y)
>      {
>          super(x, y);
>          // Do anything else here if you want
>      }
> }

That does of course work, but if you have 15 different constructors, and  
most of them are identical to those of the base class, the template mixin  
is simpler and cleaner.

-- Simen
March 04, 2008
Re: constructor inheritance
== Quote from Simen Kjaeraas (simen.kjaras@gmail.com)'s article
> On Tue, 04 Mar 2008 22:25:15 +0100, Robert Fraser
> <fraserofthenight@gmail.com> wrote:
> > How about just:
> >
> > class Child : Parent
> > {
> >      this(int x, int y)
> >      {
> >          super(x, y);
> >          // Do anything else here if you want
> >      }
> > }
> That does of course work, but if you have 15 different constructors, and
> most of them are identical to those of the base class, the template mixin
> is simpler and cleaner.

Template mixin requires the creator of the base class to have planned ahead, however.  In D 2.0 it may
be possible to fake this using a string mixin and the new type attributes stuff, but I think the idea of
inheritable ctors provides enough general utility that it should be a language feature.  As mentioned in
my original proposal, it provides for an entirely new programming idiom (new to my knowledge
anyway).


Sean
March 05, 2008
Re: constructor inheritance
Elwis wrote:
> Ary Borenszweig Wrote:
> 
>> Elwis wrote:
>>> I might have described my problem unclearly.
>>>
>>> I has root class and it has some constructor. One of those just calls one of its methods. I'd like not to copy declaration of this constructor in all of its children.
>> Do you mean you don't want to have to do this?
>>
>> class Parent {
>>
>>    this(int x, int y) {
>>      // some code
>>    }
>>
>> }
>>
>> class Child : Parent {
>>
>>    // I wish the compiler would add the this(int x, int y) constructor
>>    // automatically for me here
>>
>> }
> 
> It doesn't work. Maybe it isn't supported by GDC?

Sorry for the misunderstanding. I didn't expect that to work, I was just 
trying to see if that's what you wanted the compiler to do.

Anyway, I like Sean Kelly's proposal. But, if you have 15 constructors, 
the problem may not be the language but a design flaw. Could you show in 
which particular case you need to inherit constructors?
March 05, 2008
Re: constructor inheritance
On 05/03/2008, Ary Borenszweig <ary@esperanto.org.ar> wrote:
> Could you show in
> which particular case you need to inherit constructors?

Exception.

Nuff said. :-)
March 05, 2008
Re: constructor inheritance
Janice Caron wrote:
> On 05/03/2008, Ary Borenszweig <ary@esperanto.org.ar> wrote:
>> Could you show in
>> which particular case you need to inherit constructors?
> 
> Exception.
> 
> Nuff said. :-)

That's a good one.

However, I was asking for his specific needs, not a general example.
March 05, 2008
Re: constructor inheritance
I've got some classes and all of those can be created with one of two methods. First is creating by assigning values from arguments of constructor and second assigns values from string and that is done with method that is needed by interface( but unfortunately it can't contain constructor)
Next ›   Last »
1 2
Top | Discussion index | About this forum | D home