The Right Approach to Exceptions

H. S. Teoh hsteoh at quickfur.ath.cx
Mon Feb 20 12:33:50 PST 2012


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

-- 
Жил-был король когда-то, при нём блоха жила.


More information about the Digitalmars-d mailing list