The Right Approach to Exceptions

kennytm kennytm at gmail.com
Mon Feb 20 16:44:11 PST 2012


"H. S. Teoh" <hsteoh at quickfur.ath.cx> wrote:
> On Mon, Feb 20, 2012 at 03:17:44PM -0500, Nick Sabalausky wrote:
> [...]
>> 2. The solution fails to cover the *entire* scope of the *real* problem: 
>> Classes that need to write boilerplate ctors which merely forward to the 
>> base class's ctors.  This issue is *not* limited to Exceptions, but Andrei's 
>> proposes solution *only* covers the case with Exceptions.
>> 
>> A better solution has already been proposed:
>> 
>>     class AcmeException : Exception
>>     {
>>         mixin inheritCtors!();  // Actual name open for bikeshedding
>>     }
> 
> Somewhere in this giant thread, somebody has proposed that there should
> be language support for implicitly inheriting base class ctors. That is,
> if you write:
> 
> 	class Base {
> 		this(string, int, float) { ... }
> 	}
> 
> 	class Derived : Base { }
> 
> then the compiler will automatically insert this into Derived:
> 
> 	this(string s, int i, float f) {
> 		super(s, i, f);
> 	}
> 
> I think this is a very useful mechanism that should be added to D. It
> will eliminate a lot of ctor boilerplate that you have to put in derived
> classes (not just exceptions, but in general). For example, if you have:
> 
> 	class Base {
> 		this(string) {...}
> 		this(int) {...}
> 		this(float) {...}
> 	}
> 
> then currently, *every single derived class* must write wrappers to
> forward ctor arguments to each of those ctors, if they want to provide
> the same construction API as the base class. With the proposed
> enhancement, you'd just "inherit" all of the ctors automatically. (Not
> in the strict sense of inherit, but the compiler will create forwarding
> ctors for you automatically.)
> 
> 
> T

Note that this should work:

class Derived : Base {
  this(T...)(T values) { 
    super(values);
  }
}


More information about the Digitalmars-d mailing list