Thread overview
Overriding final functions
Mar 20, 2007
Ary Manzana
Mar 20, 2007
Ary Manzana
March 20, 2007
The following code:

class X {
	
	public final void bla() {
	}
	
}

class Y : X {
	
	public override void bla() {
	}
	
}

gives:

main.d(10): function main.Y.bla function bla does not override any

I'd expect the error to be:

main.d(10): function main.Y.bla cannot override final function main.X.bla

(which I do get if I put the "final" keyword in bla on class Y)

Am I misunderstanding something or this is a bug?
March 20, 2007
Ary Manzana wrote:
> The following code:
> 
> class X {
>         public final void bla() {
>     }
>     }
> 
> class Y : X {
>         public override void bla() {
>     }
>     }
> 
> gives:
> 
> main.d(10): function main.Y.bla function bla does not override any
> 
> I'd expect the error to be:
> 
> main.d(10): function main.Y.bla cannot override final function main.X.bla
> 
> (which I do get if I put the "final" keyword in bla on class Y)
> 
> Am I misunderstanding something or this is a bug?

Its not technically a bug, per se, although I agree with you on what would have been the more intuitive error message -- and perhaps that should be filed as a suggestion.  The general logic behind seems to be this: 'override' usurps virtual methods, and 'final' makes a method non-virtual.  So for the internal checks of 'override' there is no '/*(virtual)*/ void bla()' to match.

Slightly screwy, but understandable.  I would think it straightforward to add a last-resort check for matching 'final'/non-virtual methods.

-- Chris Nicholson-Sauls
March 20, 2007
Chris Nicholson-Sauls escribió:
> Ary Manzana wrote:
>> The following code:
>>
>> class X {
>>         public final void bla() {
>>     }
>>     }
>>
>> class Y : X {
>>         public override void bla() {
>>     }
>>     }
>>
>> gives:
>>
>> main.d(10): function main.Y.bla function bla does not override any
>>
>> I'd expect the error to be:
>>
>> main.d(10): function main.Y.bla cannot override final function main.X.bla
>>
>> (which I do get if I put the "final" keyword in bla on class Y)
>>
>> Am I misunderstanding something or this is a bug?
> 
> Its not technically a bug, per se, although I agree with you on what would have been the more intuitive error message -- and perhaps that should be filed as a suggestion.  The general logic behind seems to be this: 'override' usurps virtual methods, and 'final' makes a method non-virtual.  So for the internal checks of 'override' there is no '/*(virtual)*/ void bla()' to match.
> 
> Slightly screwy, but understandable.  I would think it straightforward to add a last-resort check for matching 'final'/non-virtual methods.
> 
> -- Chris Nicholson-Sauls

The strange thing is that in the semantic code you have:

if (isFinal()) {
  ...
  // other checks
  ...
  error("...cannot override final...");
  ...
}

So it seems that message would only show if the overriding function also has the final modifier. Strange behaviour...