digitalmars.D - constructor inheritance
- Elwis (2/2) Mar 04 2008 Hi.
- Simen Kjaeraas (20/24) Mar 04 2008 A class constructor in D /is/ inherited automatically. From the spec
- Elwis (2/2) Mar 04 2008 I might have described my problem unclearly.
- Extrawurst (3/6) Mar 04 2008 If by child of a class you mean a derived class who inherits from root
- Ary Borenszweig (11/14) Mar 04 2008 Do you mean you don't want to have to do this?
- Elwis (2/23) Mar 04 2008 It doesn't work. Maybe it isn't supported by GDC?
- Simen Kjaeraas (24/49) Mar 04 2008 It is not supported by D at all. At least not at the moment. Like I said...
- Robert Fraser (10/67) Mar 04 2008 How about just:
- Simen Kjaeraas (6/15) Mar 04 2008 That does of course work, but if you have 15 different constructors, and...
- Sean Kelly (7/22) Mar 04 2008 Template mixin requires the creator of the base class to have planned ah...
- Ary Borenszweig (6/30) Mar 05 2008 Sorry for the misunderstanding. I didn't expect that to work, I was just...
- Janice Caron (3/5) Mar 05 2008 Exception.
- Ary Borenszweig (3/10) Mar 05 2008 That's a good one.
- Elwis (1/1) Mar 05 2008 I've got some classes and all of those can be created with one of two me...
- Sean Kelly (3/3) Mar 04 2008 Not currently. I posted a proposal for this maybe 6-8 months ago and go...
- Sean Kelly (5/7) Mar 04 2008 Here's the link:
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
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
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
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
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
Ary Borenszweig Wrote:Elwis wrote:It doesn't work. Maybe it isn't supported by GDC?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
On Tue, 04 Mar 2008 21:59:35 +0100, Elwis <elwispl gmail.com> wrote:Ary Borenszweig Wrote: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; }Elwis wrote:It doesn't work. Maybe it isn't supported by GDC?I might have described my problem unclearly. I has root class and it has some constructor. One of those just callsone 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
Simen Kjaeraas wrote:On Tue, 04 Mar 2008 21:59:35 +0100, Elwis <elwispl gmail.com> wrote:How about just: class Child : Parent { this(int x, int y) { super(x, y); // Do anything else here if you want } }Ary Borenszweig Wrote: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; }Elwis wrote:It doesn't work. Maybe it isn't supported by GDC?I might have described my problem unclearly. I has root class and it has some constructor. One of those justcalls 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
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
== Quote from Simen Kjaeraas (simen.kjaras gmail.com)'s articleOn Tue, 04 Mar 2008 22:25:15 +0100, Robert Fraser <fraserofthenight gmail.com> wrote: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). SeanHow 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.
Mar 04 2008
Elwis wrote:Ary Borenszweig Wrote: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?Elwis wrote:It doesn't work. Maybe it isn't supported by GDC?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 05 2008
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
Janice Caron wrote:On 05/03/2008, Ary Borenszweig <ary esperanto.org.ar> wrote:That's a good one. However, I was asking for his specific needs, not a general example.Could you show in which particular case you need to inherit constructors?Exception. Nuff said. :-)
Mar 05 2008
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
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
== Quote from Sean Kelly (sean invisibleduck.org)'s articleNot currently. I posted a proposal for this maybe 6-8 months ago and got no response. I'd love to haveitin D 2.0.Here's the link: http://www.digitalmars.com/d/archives/digitalmars/D/Inheriting_constructors_54088.html Sean
Mar 04 2008