[Issue 2091] D2 final cannot be applied to variable

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Tue Jun 2 21:31:38 PDT 2015


https://issues.dlang.org/show_bug.cgi?id=2091

Kenji Hara <k.hara.pg at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Hardware|x86                         |All
            Version|2.013                       |D2
                 OS|Windows                     |All
           Severity|normal                      |major

--- Comment #3 from Kenji Hara <k.hara.pg at gmail.com> ---
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.

--


More information about the Digitalmars-d-bugs mailing list