When does final mean "maybe, kinda, sorta" ?

kris foo at bar.com
Mon Jan 22 15:31:25 PST 2007


"That's a great deal to make one word mean," Alice said in a thoughtful 
tone. "When I make a word do a lot of work like that," said Humpty 
Dumpty, "I always pay it extra."


Reading one of Andrei's posts about const and final reminded me of what 
I consider to be a D wart. This is taken directly from the D reference 
manual:

"Functions marked as final may not be overridden in a derived class, 
unless they are also private."

Just how much does "final" get paid for this? Onto the given example:


<quote>

class A
{
     int def() { ... }
     final int foo() { ... }
     final private int bar() { ... }
     private int abc() { ... }
}

class B : A
{
     int def() { ... }	// ok, overrides A.def
     int foo() { ... }	// error, A.foo is final
     int bar() { ... }	// ok, A.bar is final private, but not virtual
     int abc() { ... }	// ok, A.abc is not virtual, B.abc is virtual
}

void test(A a)
{
     a.def();	// calls B.def
     a.foo();	// calls A.foo
     a.bar();	// calls A.bar
     a.abc();	// calls A.abc
}

void func()
{   B b = new B();
     test(b);
}

</quote>


1) You had to read that at least twice, didn't you? :)

2) Seems to me the usage of "final" here is entirely misleading, and 
opens up a lurking hole for some poor sod to break their ankle in.

3) the example (from the ref manual) highlights overriding of private 
and private-final methods. Surely this applies only within module scope? 
That is, within the same module? Assuming that's the case, why would any 
sane engineer do such a thing instead of changing the original class 
code? They have the source, obviously, and overriding private seems 
quite dubious at best. I can think of one case, but can't see why the 
language would need to support it explicitly.

In short, it appears the example exhibits some poor practice (overriding 
private methods), is somewhat misleading (one certainly *hopes* private 
cannot be seen beyond module-scope), and discusses how to make "final" 
mean something quite other than final.

How about fixing final? Or name it something more appropriate? It really 
shouldn't get paid any more than other keywords ...

- Kris



More information about the Digitalmars-d mailing list