How should overruling of overloaded functions work?

Kristian kjkilpi at gmail.com
Tue Aug 22 09:05:55 PDT 2006


OOOKAY! You're right, that does not work! :( :( :(

There was a stupid error in my test case... and I was too quick to get out  
of my house running and yelling "it works!" to people! (When police  
appeared, I was forced to stop.)

One word: aaargh!
It was too good to be true anyway... :(

I just don't get it.
This is clearly a case where overload functions should not be hidden! I  
mean, if a function is final, you have to be absolutely sure that there is  
no need to change (i.e. override) it in a subclass.

If you cannot change it in a subclass, then there is no harm in changing  
its implementation in the base class. Subclasses will work as normal after  
the change. If not, then the function must not be final!


Thank you Walter, and others, for this great language.
... But *please* don't left these kind of glitches in it! Little details  
are important also, programmers have to deal with them all the time!


On Tue, 22 Aug 2006 17:15:56 +0300, Bruno Medeiros  
<brunodomedeiros+spam at com.gmail> wrote:
> Kristian wrote:
>> On Sat, 19 Aug 2006 18:09:25 +0300, Kristian <kjkilpi at gmail.com> wrote:
>>> 3)
>>> If the overloading does not work for derived classes, then the  
>>> following common case does not work without the alias hack:
>>>
>>> class String {...}
>>>
>>> class Base {
>>>      void f(String s) {...}
>>>      //these functions are provided for convenience...
>>>      final void f(int v) {
>>>          f(new String(v));
>>>          }
>>>      final void f(float v) {
>>>          f(new String(v));
>>>          }
>>> }
>>>
>>> class Derived : Base {
>>>      void f(String s) {...}  //overrule the main function that does  
>>> all the work
>>> }
>>   Okey, okey, why doesn't someone tell me that this actually works!? :)
>>  The functions 'f(int)' and 'f(float)' are not hidden from Derived  
>> because they're final. Nice!
>>  This was the main reason I protested against the current overload  
>> function hiding. But because it do work, I will fell silent (and look a  
>> bit embarrassed). ;)
>>  This enables me to write these convenience functions without the need  
>> of rewriting (or alias hacking) them in subclasses. Non-final functions  
>> should be reimplemented anyway (if someone wants to use alias hacking  
>> for those, be my guest).
>
> Are you sure it works? It's not working for me, I tried the following  
> code:
>
> -----
> import std.stdio;
>
> class String {}
>
> class Base {
>      void f(String s) {
>      	writefln("Base.f(String)");
>      }
>      //these functions are provided for convenience...
>      final void f(int v) {
>          writefln("Base.f(int)");
>      }
>      final void f(float v) {
>          writefln("Base.f(float)");
>      }
> }
>
> class Derived : Base {
>      void f(String s) { writefln("Derived.f"); }
> }
>
> void test() {
>      Derived obj = new Derived;
>      obj.f(10);  // ERROR, doesn't work
> }
>




More information about the Digitalmars-d mailing list