The following code compiles on D1 but fails on D2. I think 'final:' is being applied to the whole class.
test.d
------
class Bar { }
class Foo {
final:
this() {}
Bar getBar() {
return b;
}
private:
Bar b;
}
void main() {
}
c:\> dmd test.d
test.d(11): variable c.Foo.b final cannot be applied to variable
Comment #1 by bugzilla — 2008-05-11T04:25:30Z
final with a : applies until the closing } of the declaration block. That covers the declaration of b.
The error message is correct. Not a bug.
Comment #2 by kozzi11 — 2015-04-02T12:29:39Z
No, i would say it is a bug and a it is a really annoying problem for many people I works with.
Because methods in D are virtual by default, so using
class A { final:
//final methods
}
is really common pattern in our codebase
(OK to be fair we use a little modified pattern)
class A {
// members
// virtual methods
final:
//final methods
}
But after there will be a way how to marks methods as non final (virtual, final!false ...)
It makes it easier, one just need to write:
class A { final:
instead of:
class A {
I really does not see any reason why final is applied on members
Comment #3 by k.hara.pg — 2015-06-03T04:31:38Z
Today, applying final attribute to a variable makes following error.
final int x;
// Error: variable x final cannot be applied to variable, perhaps you meant const?
In Java, there's a "final variable" for read only data, that cannot be modified after its initialization.
In D, we have 'const' for read only data. Therefore the diagnostic message is friendly for the newbies who came from Java.
But, by using label or block style attribute syntax, unintentionally some class member variables could be final.
class C1 {
final {
void foo() {} // yes, it's final method
int x; // final variable!?
}
}
class C2 {
final:
void foo() {} // ditto
int x; // ditto!?
}
Therefore it's rather harmful limitation to the layout of class member.
=======
To relax the limitation, compiler can just ignore final attribute on variable, excepting prefix style.
// Proposed behavior
class C
{
final int x; // error, variable x cannot be final
final { int y; } // no error
final: int z; // no error
}
The proposal can be used also for override, abstract, and synchronized attributes.