inheriting constructos

Denis Koroskin 2korden at gmail.com
Sun Nov 29 15:58:51 PST 2009


On Mon, 30 Nov 2009 02:20:40 +0300, bearophile <bearophileHUGS at lycos.com>  
wrote:

> Andrei Alexandrescu:
>> c) If a class doesn't define any constructors but does add at least a
>> non-static field -> undecided.
>
> Does 'undecided' mean 'compile-time error'?"
>
> Bye,
> bearophile

I think it means they are not decided whether it should inherit  
constructors.

Back on topic, I do think inheriting constructors is a good idea, even in  
presence of additional fields (why not?)

I also think constructor inheritance could be implemented without any  
changes to the language the following way:

this(Args...)(Args args) if (__traits(compiles, super(args)))
{
     super(args);

     // initialize additional fields, if any present
     // and/or do some post-construction logic
}

Why create new rules? :)

The trivial way could be even simplified like this:

// just a basic idea
template InheritCtors()
{
     this(Args...)(Args args) if (__traits(compiles, super(args)))
     {
         super(args);
         static assert (this.tupleof == super.typleof); // check that no  
additional members present
     }
}


class Foo { /* a few different ctors */ }

class Bar : Foo
{
     mixin InheritCtors(); // Voilà!
}



More information about the Digitalmars-d mailing list