Automatic method overriding in sub-classes

bitwise via Digitalmars-d digitalmars-d at puremagic.com
Tue Oct 27 07:47:01 PDT 2015


On Tuesday, 27 October 2015 at 14:21:12 UTC, bitwise wrote:
> On Tuesday, 27 October 2015 at 07:52:27 UTC, Jacob Carlborg 
> wrote:
>> On 2015-10-27 00:25, Tofu Ninja wrote:
>>> [...]
>>
>> I don't think this is possible. Think of code looking like 
>> this:
>>
>> // Imagine not having access to the source code "createA"
>> A createA()
>> {
>>     new B;
>> }
>>
>> void inspectA(A a)
>> {
>>     a.bar();
>> }
>>
>> How should the compiler know that when instantiating/calling 
>> "bar", T should be set to B? The compiler might not even know 
>> about B exists at all. Even if the compiler does have access 
>> to the complete source code it would, most likely, need to do 
>> a full program analyze to figure out the type of T, which is 
>> quite complicated.
>
> It's not a template function. The auto override wouldn't be 
> instantiated if/when it's called. It would always be 
> instantiated automatically when the class was compiled. Then, 
> if you were just using the class as an import, the compiler 
> would know that the definition should exists somewhere(static 
> lib, etc..).
>
> When an auto override function was called, it may or may not 
> have the definition..it wouldn't matter, since it's like a 
> virtual function. But, if someone tried to subclass a class 
> which had even one auto override function with missing it's 
> definition, then the effect would be the same as trying to 
> subclass a sealed/final class.
>
>     Bit

Going a bit further, I think you could override an auto override 
fun too manually as well. This would be helpful both for adding 
additional functionality, and as a fallback if the definition of 
the auto override function was not available. If someone sub 
classed a class in which they had manually overridden an auto 
override function, the compiler would fall back to using the base 
method, if available. Finally(pun intended), a user could 
manually override an auto override function and mark it as final 
to stop the automatic overriding.

    Bit


More information about the Digitalmars-d mailing list