aliasing base methods

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Sun Feb 25 05:48:52 PST 2007


Frank Benoit (keinfarbton) wrote:
> Why do we really need this strange overloading, reimplementation rules.
> It is so annoying.
 >
> If you overload a method from a base class you hide the base method. To
> avoid this you need to do an alias.
> alias Base.fnc fnc;

It can be a bit annoying at times, yes.

> If more than one version of fnc exists, you cannot specify which one
> shall be alias.

If you aliased an overloaded function or method name, you alias *all* 
the overloaded versions:
---
urxae at urxae:~/tmp$ cat test.d
import std.stdio;

class Base {
     void foo(int) { writefln("foo(int)"); }
     void foo(char[]) { writefln("foo(char[])"); }
}

class Derived : Base {
     void foo(float) { writefln("foo(float)"); }
     alias Base.foo foo;
}

void main(){
     Derived d = new Derived;
     d.foo(1);
     d.foo("bar");
     d.foo(1.0);
}
urxae at urxae:~/tmp$ dmd -run test.d
foo(int)
foo(char[])
foo(float)
---

Judging by your views on the rest of this matter, I think you will agree 
that this is usually what you want to do.

However, this seems to be another manifestation of the inability to 
specify specific overloads that is also so annoying when trying to take 
a function pointer (or in this case delegate) of an overloaded 
function/method.
A syntax for this has been proposed (e.g. &d.foo(int)) but I don't 
recall Walter responding to it. (If he did, it probably wasn't 
positively or I would have remembered)

> There is no advantage in this. Without this rule, you can probably
> overload instead of the intended override (But we already have the
> override keyword, make it required?). With this rule, you probably hide
> an existing base implementation and probably change the behaviour of the
> class, if the overloaded function is compatible to the base version.
> (e.g. visitor pattern).


> Another pain point: Why do we really need to reimplement a method if an
> interface is again used?
> interface I{ void fnc(); }
> class B : I { void fnc(){} }
> class C : B, I { } // error needs to reimplement fnc.

I can understand this one, actually. The way I see it, the only reason 
to reimplement an interface is to make sure it remains implemented even 
if the base class is modified. So the only way to keep it compiling if 
the interface is removed from the base class is to ensure you provide 
implementations.
On the other hand, an argument could be made that compile-time errors 
("interface method not implemented: 'I.fnc'") would be appropriate in 
this case...



More information about the Digitalmars-d mailing list