***** D method override mechanisms borked ******

Bruno Medeiros brunodomedeirosATgmail at SPAM.com
Mon Jun 26 13:45:02 PDT 2006


Sean Kelly wrote:
> Bruno Medeiros wrote:
>> kris wrote:
>>>
>>> The original behaviour limited the exposure of an overridden method 
>>> to be less than or equal to the exposure of the original. For 
>>> example, protected could not be made public via an override. The 
>>> compiler would give you an error if you attempted to do so. The 
>>> compiler used to prevent you from intercepting a superclass method 
>>> where the original intent (of the designer) was that said method 
>>> whould be internal usage only. For example, final, package, or 
>>> private methods.
>>>
>>
>> If that was the original behavior, that behavior was broken. You see, 
>> when overriding a method, limiting the protection level is unsafe, 
>> whereas widening the protection level is safe. The protection level is 
>> a call-site contract (like the return type) and as such is only safe 
>> when overridden invariantly or *covariantly*.
>>
>> If you designed your classes around that original behavior, they're 
>> broken.
> 
> I'm not sure I agree.  However, if overriding behaves this way then it 
> makes sense that aliasing should as well, and I would be surprised if 
> aliasing ever behaved this way.  Perhaps this is one area where one 
> should rely on programming style and not on the compiler?
> 
> 
> Sean

Not sure you agree with what exactly? There is no discussion to what 
kind of overriding is "protection safe" or not.
Still, as for what the language behavior should be, that's arguable (but 
I didn't comment on that).
For instance, one could allow covariant protection levels, that's the 
behavior of Java. But even though it is safe, I do find it a bit odd in 
practice.
One could allow invariant protection levels only (that's the behavior of 
C#).
And even contravariant protection levels, although not "protection 
safe", is actually a behavior that can have a certain sense. (calling it 
broken was perhaps inadequate)

-- 
Bruno Medeiros - CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D



More information about the Digitalmars-d-bugs mailing list