super constructors question

BCS BCS at pathlink.com
Mon Aug 14 09:27:15 PDT 2006


The major problem I see with this is say you have:

class Foo
{
	this(){...}
	this(char){...}
	this(char[]){...}
	... // 27 more constructors
}

class Bar : Foo
{
	// all constructors implicit
}

Now add one constructor to Bar.
All of the implicit constructors vanish. Have fun tracking that all down.

Implicit constructors violate the idea that "simple changes should be 
simple" (my position).

a Better solution might be to allow:

class Bar:Foo
{
	alias super(char[]);
	 // shorthand for: this(char[] arg){super(arg)};
}

Jarrett Billingsley wrote:
> "Serg Kovrov" <kovrov at no.spam> wrote in message 
> news:ebn850$3qa$1 at digitaldaemon.com...
> 
>>Hello everybody,
>>
>>Could someone explain why D do not lookup for appropriate super
>>constructor? It is in specs:
>>
>>>If there is no constructor for a class, but there is a constructor  for 
>>>the base class, a default constructor of the form:
>>>this() { }
> 
> 
> That really should read "this() { super(); }".
> 
> 
>>But i do not understand intension. Is it so hard for compiler to find
>>suitable constructor in base class(es)?
> 
> 
> It's not really trivial.  Basically the rule would have to change so that if 
> a class were defined without any ctors, it would have to create a ctor for 
> each ctor that the base class has, and forward the parameters.  So your code 
> would insert an implicit "this(char[] name) {super(name);}" in FooBar.  That 
> seems like a little too much automation for very rarely any real benefit. 
> Most of the time you're going to be making new constructors for the derived 
> class anyway, so it's kind of a niche problem. 
> 
> 



More information about the Digitalmars-d-learn mailing list