Making inheritance less tedious
janderson
askme at me.com
Mon Feb 26 08:53:55 PST 2007
Kristian Kilpi wrote:
>
> I think class inheritance is a bit(?) tedious sometimes (that is,
> unnecessarily tedious).
> (I leave interfaces out of this discussion.) 'Problems', IMHO, with it are:
>
> 1) You cannot inherit constructors.
>
> 2) A function will hide inherited functions having the same name.
>
> 3) The syntax for calling a super function of the base class is
> redundant and (could be) tedious.
>
> For the case 2 there is an 'alias hack' (e.g. 'alias Base.foo foo;'),
> but it's redundant and ugly.
> Recently this issue was discussed in the thread 'aliasing base methods'
> started by Frank Benoit
> (http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=49572),
> so I don't
> talk about it here.
>
>
> There should be some way to 'break' the case 1. For example, the
> following is laborious and error-prone:
>
> class CheckBox {
> this();
> this(string label);
> this(Icon icon, string label = null);
>
> void draw();
> }
>
> class MyCheckBox : CheckBox {
> this() {
> super();
> }
> this(string label) {
> super(label);
> }
> this(Icon icon, string label = null) {
> super(icon, label);
> }
>
> void draw();
> }
>
> All I wanted was to reimplement the 'draw()' function. But I had to
> write all those redundant constructors also!
> (Imagine if there were 20 or so constructors in the base class -- yes,
> it's possible in some situations.)
> Surely there should be a simple solution for this?
>
> For example, something like this:
>
> class MyCheckBox : CheckBox {
> inherit constructors;
>
> void draw();
> }
>
> Or better yet:
>
> class MyCheckBox : inherit CheckBox {
> void draw();
> }
>
>
> What about the case 3? Well, lets have the following member function:
>
> foo(int value, int x, int y, bool is_clipped, bool is_inverted, string
> label = null) {
> ...
>
> super.foo(value, x, y, is_clipped, is_inverted, label);
> }
>
> It would be nice to have the following possible:
>
> foo(int value, int x, int y, bool is_clipped, bool is_inverted, string
> label = null) {
> ...
>
> superfunc(..); //equals to 'super.foo(value, x, y, is_clipped,
> is_inverted, label);'
> }
>
> 'superfunc(..)' (or 'superfunc($)' or something) is non-redundant, easy
> to read, and
> trivial to maintain (because no maintenance is needed! :) ).
>
> You can of course define the arguments by yourself if needed:
>
> superfunc(value + 1, x, y, false, is_inverted);
I agree about it being tedious. One ->slight<- improvement to your
problem is to use more mixins for the boilerplate code.
-Joel
More information about the Digitalmars-d
mailing list