Bug 2091 – D2 final cannot be applied to variable

Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2008-05-10T07:26:00Z
Last change time
2017-07-22T12:35:26Z
Keywords
pull, rejects-valid
Assigned to
bugzilla
Creator
gide

Comments

Comment #0 by gide — 2008-05-10T07:26:58Z
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.
Comment #4 by k.hara.pg — 2015-06-03T12:41:14Z
Comment #5 by github-bugzilla — 2015-06-23T06:58:18Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/397752ba67e2db4868865b57ddfcd6f76724f386 fix Issue 2091 - D2 final cannot be applied to variable D2 has `const` ro represent readonly data, therefore the error and suggestion "perhaps you meant const?" is normally legitimate. But, if the `final` attribute comes from a label or block style syntax, it's too restrict against class member layout. To lift the limitation, display the error only when `final` is *directly* applied on a variable. Use same method to `synchrionized`, `abstract`, and `override`. https://github.com/D-Programming-Language/dmd/commit/b9864b41576e3d20df0969e2731c19f663b55a31 Merge pull request #4714 from 9rnsr/fix2091 Issue 2091 - D2 final cannot be applied to variable
Comment #6 by github-bugzilla — 2015-10-04T18:18:27Z
Comment #7 by github-bugzilla — 2017-07-22T12:35:26Z
Commits pushed to dmd-cxx at https://github.com/dlang/dmd https://github.com/dlang/dmd/commit/397752ba67e2db4868865b57ddfcd6f76724f386 fix Issue 2091 - D2 final cannot be applied to variable https://github.com/dlang/dmd/commit/b9864b41576e3d20df0969e2731c19f663b55a31 Merge pull request #4714 from 9rnsr/fix2091