www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - ctor not inhrited in a classes

reply Zaher Dirkey <zaherdirkey yahoo.com> writes:
I discussed this on IRC before years but i didnt get good answer.

Let assume I have library that used by my coworkers

In the example bellow my coworkers used SubBaseClass to create 
their classes, in first version of my code I dont have ctor with 
arg, and everything is fine,
but later i added parent to it and i want SubBaseClass and (child 
  classes) not created without parent value, i want to force them 
to do that.

what happen the new ctor not inhrited to child classes, and 
coworkers still can create it using (new) without parameters, we 
have alot of child classes,
I want compiler stop compiling if parent not defined.


import std.stdio;

class SuperClass {
     this(){

     }
}


class SubBaseClass: SuperClass {

     this(){

     }

     SubBaseClass parent;

     this(SubBaseClass Parent) {
       //some code to use parent etc
       writeln("childclass ctor");
     }
}

//CoWorkers or who are using my lib

class ChildBaseClass1: SubBaseClass {
}

class ChildBaseClass2: SubBaseClass {
}


void main()
{
	//compiled
     ChildBaseClass1 a = new ChildBaseClass1();
     ChildBaseClass2 b = new ChildBaseClass2();

     //error:is not callable using argument types
     //but i need to force to use to init varables
     ChildBaseClass1 a = new ChildBaseClass1(null);
     ChildBaseClass2 b = new ChildBaseClass2(a);

}
Jun 16
parent reply Basile B. <b2.temp gmx.com> writes:
On Sunday, 16 June 2019 at 14:11:15 UTC, Zaher Dirkey wrote:
 I discussed this on IRC before years but i didnt get good 
 answer.

 Let assume I have library that used by my coworkers

 In the example bellow my coworkers used SubBaseClass to create 
 their classes, in first version of my code I dont have ctor 
 with arg, and everything is fine,
 but later i added parent to it and i want SubBaseClass and 
 (child
  classes) not created without parent value, i want to force 
 them to do that.

 what happen the new ctor not inhrited to child classes, and 
 coworkers still can create it using (new) without parameters, 
 we have alot of child classes,
 I want compiler stop compiling if parent not defined.


 import std.stdio;

 class SuperClass {
     this(){

     }
 }


 class SubBaseClass: SuperClass {

     this(){

     }

     SubBaseClass parent;

     this(SubBaseClass Parent) {
       //some code to use parent etc
       writeln("childclass ctor");
     }
 }

 //CoWorkers or who are using my lib

 class ChildBaseClass1: SubBaseClass {
 }

 class ChildBaseClass2: SubBaseClass {
 }


 void main()
 {
 	//compiled
     ChildBaseClass1 a = new ChildBaseClass1();
     ChildBaseClass2 b = new ChildBaseClass2();

     //error:is not callable using argument types
     //but i need to force to use to init varables
     ChildBaseClass1 a = new ChildBaseClass1(null);
     ChildBaseClass2 b = new ChildBaseClass2(a);

 }
There was a DIP proposing better inhertence of constructors. Unfortunately it's been dropped : https://github.com/dlang/DIPs/blob/master/DIPs/other/DIP1004.md Meaning that someone need to replace the original author(s). Maybe something based on mixin can help in your case, even if that's not clean IMO.
Jun 16
parent Zaher Dirkey <zaherdirkey yahoo.com> writes:
On Sunday, 16 June 2019 at 20:43:05 UTC, Basile B. wrote:
 On Sunday, 16 June 2019 at 14:11:15 UTC, Zaher Dirkey wrote:

 There was a DIP proposing better inhertence of constructors. 
 Unfortunately it's been dropped : 
 https://github.com/dlang/DIPs/blob/master/DIPs/other/DIP1004.md
 Meaning that someone need to replace the original author(s).

 Maybe something based on mixin can help in your case, even if 
 that's not clean IMO.
After looked at the example, Now I understand why they go with mixin as a solution, in c++11 struct D : B { using B::B; // inherit constructors from B }; Here we inherited the ctor by modifiing derived class (subclass) not superclass, mixin will do the same, and that not a good solution, Why i need i dont want to modify sub classes to inherite ctor, We need to make ctor already inherited to all subclasses and subsubclases, and stop compiling if there is no right parameters passed to it. While I am a newbie in D (I think i will be stay newbie) not sure if a feature request acceptable from me. mayb adding new feature like disable, but somting inherite (or better attrubute) to the ctor class SuperClass { inhrite this(int x){ } } not this ctor will apply to all subclasses and blew sub sub classes. my question: if there is a magic trick in D to do that, i know D have amazing magics.
Jun 17