rename this to ctor

Christian Kamm kamm-incasoftware at shiftatleftanddelete.de
Wed Apr 1 03:36:56 PDT 2009


Walter Bright Wrote:
> Instead, you can do this:
> 
> class inherit : base
> {
>      ctor(int i) { }
>      ctor() { super(); }
> }

Yes, but it gets uncomfortable when you want to forward more constructors:

class base {
  ctor(int never, MyTemplate!(float, 3, "abcd") ending, char[] parameter = "abcd", int list = 3) {}
  ctor(float including, int defaults = 3) {}
}

class derived : base {
  ctor(int never, MyTemplate!(float, 3, "abcd") ending, char[] parameter = "abcd", int list = 3) 
  { super(never, ending, parameter, list); }
  ctor(float including, int defaults = 3) 
  { super(including, defaults); }
}

plus you have to replicate default values and make sure you update all derived classes when changing the base constructors. I.e. the same arguments that explain why you can use alias to bring base class member functions into the overload set apply to the constructor. Also, sometimes the base class constructors are unknown:

class C(T) : T { this(?) { super(?); }

I guess these issues could also be solved using a mixin if there was a way to enumerate constructors at compile time, but there's another advantage to renaming 'this' to a non-keyword name: you allow getting its address and using it as a template alias parameter.




More information about the Digitalmars-d mailing list