Proposition for change in D regarding Inheriting overloaded methods

Steven Schveighoffer schveiguy at yahoo.com
Thu Aug 9 07:56:58 PDT 2007


"Christopher Wright" <dhasenan at gmail.com> wrote in message 
news:f9evna$284k$1 at digitalmars.com...
> Steven Schveighoffer wrote:
>> If there is a class A, which is a base class of class B, where A defines 
>> a method foo(args),  and B does not define foo(args), then the compiler 
>> should fail to compile B indicating that the user must define B.foo(args) 
>> by override or by alias.
>
> And if you don't want to provide access to those overloads? If, for 
> instance, the new overload computes something that was previously passed 
> in as an argument, but due to your changes, you cannot compute that 
> argument. With your solution, I'd have to override that method and have it 
> throw an exception. And put it in the docs that the method shouldn't be 
> used.

You can't "hide" it, it can be called by casting to the base class.  I see 
no issue with you having to override the method and throw an exception, why 
is this solution not good enough?  It is the default solution that Walter is 
trying to promote.  However, in my opinion, I find it unlikely that an 
author would want this behavior, so it should not be the default.  I would 
have to see an example to understand it more, but I think I'd probably 
suggest an alternative to overriding and throwing an exception (such as not 
deriving but defining a new class instead).  Can you give me an actual 
example?  I still have never seen one.

>
> How about having a strong and weak override? A strong override hides all 
> overloads of the function it overrides, while a weak one hides only the 
> overload that matches it. Then it's unambiguous, and only a matter of 
> defining syntax. After all, Walter doesn't implicitly know whether each 
> individual programmer wants to allow inherited overloads to work, and 
> neither does dmd, unless they're explicitly told.

As I said, you can't hide the override, but as a compromise, what if there 
were a way you could alias any undefined overrides to throw exceptions 
instead of call the base class?  Something like:

alias not_implemented foo;

This saves 1) having to code a silly override for each one you wish to 
"hide", and 2) having multiple copies of identical code which simply throws 
an exception compiled into your class.

I tried doing this with the current compiler by defining a function:

void not_implemented(...)

But the problem is that it allows one to compile code which calls overrides 
that didn't exist in a base class :)  I think the compiler would have to 
handle this case in a special way.  You would probably want the exception to 
say something like "class.foo(arg type) not defined," not sure how one would 
do this without adding a feature to the compiler.

-Steve 





More information about the Digitalmars-d mailing list