www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - constructor inheritance

reply Elwis <elwispl gmail.com> writes:
Hi.
Is there any way to make constructor inherited automaticaly. I has many classes
that have to have the same constructor, and I'd like not to copy it for each
one.
Mar 04 2008
next sibling parent reply "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On Tue, 04 Mar 2008 19:32:11 +0100, Elwis <elwispl gmail.com> wrote:

 Hi.
 Is there any way to make constructor inherited automaticaly. I has many  
 classes that have to have the same constructor, and I'd like not to copy  
 it for each one.

A class constructor in D /is/ inherited automatically. From the spec (http://www.digitalmars.com/d/2.0/class.html): "If no call to constructors via this or super appear in a constructor, and the base class has a constructor, a call to super() is inserted at the beginning of the constructor." If that for some reason does not work (it does for me), file a bug report, and use an explicit call to super() in your constructors. If you're speaking of several non-related classes having identical constructors, I'd do it with a mixin. template myConstructor { // do stuff here } this() { mixin(myConstructor); } If I have somehow completely missed your point, please do tell. -- Simen
Mar 04 2008
next sibling parent reply Elwis <elwispl gmail.com> writes:
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.
Mar 04 2008
next sibling parent Extrawurst <spam extrawurst.org> writes:
If by child of a class you mean a derived class who inherits from root 
please read the earlier post of Simen again, there is your answer.

Elwis schrieb:
 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.
   

Mar 04 2008
prev sibling parent reply Ary Borenszweig <ary esperanto.org.ar> writes:
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 }
Mar 04 2008
parent reply Elwis <elwispl gmail.com> writes:
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?
Mar 04 2008
next sibling parent reply "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On Tue, 04 Mar 2008 21:59:35 +0100, Elwis <elwispl gmail.com> 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  

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?

It is not supported by D at all. At least not at the moment. Like I said, your best bet is templates. Something like template constructors() { this() { // do stuff here } this(int x, int y) { // do stuff here } } class Parent { mixin constructors; } class Child { mixin constructors; }
Mar 04 2008
parent reply Robert Fraser <fraserofthenight gmail.com> writes:
Simen Kjaeraas wrote:
 On Tue, 04 Mar 2008 21:59:35 +0100, Elwis <elwispl gmail.com> 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 

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?

It is not supported by D at all. At least not at the moment. Like I said, your best bet is templates. Something like template constructors() { this() { // do stuff here } this(int x, int y) { // do stuff here } } class Parent { mixin constructors; } class Child { mixin constructors; }

How about just: class Child : Parent { this(int x, int y) { super(x, y); // Do anything else here if you want } }
Mar 04 2008
parent reply "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
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
Mar 04 2008
parent Sean Kelly <sean invisibleduck.org> writes:
== 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
      }
 }

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
Mar 04 2008
prev sibling parent reply Ary Borenszweig <ary esperanto.org.ar> writes:
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.

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?
Mar 05 2008
next sibling parent Ary Borenszweig <ary esperanto.org.ar> writes:
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.
Mar 05 2008
prev sibling parent Elwis <elwispl gmail.com> writes:
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)
Mar 05 2008
prev sibling parent "Janice Caron" <caron800 googlemail.com> writes:
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. :-)
Mar 05 2008
prev sibling parent reply Sean Kelly <sean invisibleduck.org> writes:
Not currently.  I posted a proposal for this maybe 6-8 months ago and got no
response.  I'd love to have it
in D 2.0.


Sean
Mar 04 2008
parent Sean Kelly <sean invisibleduck.org> writes:
== Quote from Sean Kelly (sean invisibleduck.org)'s article
 Not currently.  I posted a proposal for this maybe 6-8 months ago and got no
response.  I'd love to have

 in D 2.0.

Here's the link: http://www.digitalmars.com/d/archives/digitalmars/D/Inheriting_constructors_54088.html Sean
Mar 04 2008