The Right Approach to Exceptions

Jonathan M Davis jmdavisProg at gmx.com
Sat Feb 18 17:23:01 PST 2012


On Saturday, February 18, 2012 17:20:41 H. S. Teoh wrote:
> On Sun, Feb 19, 2012 at 01:10:10AM +0000, Ben Davis wrote:
> [...]
> 
> > I guess this is a bit off topic, but what you probably want is
> > syntactic sugar that says "declare constructors matching all super
> > constructors and calling through to them" and can be used in ANY class
> > hierarchy (not necessarily exceptions). For example:
> > 
> > class Subtype : Supertype {
> > 
> >     super all;
> > 
> > }
> > 
> > If you want to expose just specific constructors, then there could
> > also be a shorthand for "declare a constructor matching a specific
> > super constructor and calling through to it" - so you don't have to
> > repeat all the arguments. For example:
> > 
> > class Subtype : Supertype {
> > 
> >     super();
> >     super(string,int);
> > 
> > }
> > 
> > That would then make it an entirely separate issue and completely
> > not Exception-specific.
> 
> +1.
> 
> This is definitely something not specific to Exception. Quite often, you
> want to create a derived class overriding just one or two members of the
> base class, but end up having to copy-n-paste most of the many ctors in
> the base class along with their elaborate arguments just so you can pass
> through the arguments to them.
> 
> Having a way of simply saying "the ctors in this class default to the
> base class ctors" will solve this problem in a very nice and logical
> way. (I.e., you're sortof "inheriting the base class ctors", although
> not exactly, of course.)

Feel free to open a pull request. It would certainly be a useful feature for 
reducing boilerplate code in some class hierarchies (though in many, the 
constructors in the derived class must be different, in which case it wouldn't 
work).

One potential issue though is that not all base classes necessarily share the 
same constructors. Which ones would be grabbed? The ones from the immediate 
base class? All of them? Only the ones common to all?

- Jonathan M Davis


More information about the Digitalmars-d mailing list