Overloading/Inheritance issue

Christopher Wright dhasenan at gmail.com
Sat Aug 4 06:01:24 PDT 2007


Steve Schveighoffer wrote:
> Regan Heath Wrote:
> 
>> Steve Schveighoffer wrote:
>>  > My argument is not against this, as you will note that in these
>>  > examples, both base and derived classes can match the call by some
>>  > sort of implicit conversion.  In the case where both the base class
>>  > and the derived class  match the call, I could care less if the
>>  > derived class was called instead of the base class.
>>
>> But, that's exactly the problem.
>>
>> In fact example 2 shows that we currently have the undesirable and buggy 
>> behaviour in the current D implementation, eg.
>>
> 
> Oh, I understand what you are saying.  However, undesirable behavior is unavoidable no matter which way we go.  It all depends on what you expect and what the user of the class expects.  My thought is that you have to pick one way, and just tell people that's the way it is and they have to live with it.  I'm speaking only of the case where both base and derived class can match the call exactly or through some implicit conversion.

Which one saves more work? I think the common case is to want all 
members of the base class in the derived class. Perhaps the problem is 
not having a way to hide methods from a base class? Except, as you point 
out, you can always cast to the base class and retrieve those methods.

The Java version seems simpler to use and understand. Perhaps this is a 
place where compiler warnings are appropriate -- it's certainly not an 
error to override only one overload of a function, but it's a source of 
potential bugs no matter how you cut it. Though Walter is quite opposed 
to compiler warnings, and I appreciate the cleanness of DMD's output.

> On second thought, you could pick a third option for behavior, and say that if the derived class has no explicit match, but has implicit matches, and the base class has explicit or implicit matches, the compiler should error out saying it doesn't know which one you intended.  That would mean even less obscure bugs, forcing the user of the class to pick the one they want by explicitly casting the arguments or the object.

True. But the original problem would still exist, unless D's behavior 
changed to Java's.

-cbw



More information about the Digitalmars-d mailing list