www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Overriding final functions

reply Ary Manzana <ary esperanto.org.ar> writes:
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?
Mar 19 2007
parent reply Chris Nicholson-Sauls <ibisbasenji gmail.com> writes:
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
Mar 19 2007
parent Ary Manzana <ary esperanto.org.ar> writes:
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...
Mar 20 2007