'final' variables

Andrei Alexandrescu (See Website For Email) SeeWebsiteForEmail at erdani.org
Tue Mar 20 09:31:40 PDT 2007


Lionello Lunesu wrote:
> Tyler Knott wrote:
>> Lionello Lunesu Wrote:
>>> What's the use of "final" for variables? I'm saying "for variables" 
>>> because for methods the benefit is only too clear.
>>>
>>
>> Because the "const" keyword is being repurposed for read-only 
>> references to mutable or non-mutable data, 
> 
> ...which I want, very much!
> 
>> we need a new keyword for non-mutable variables.
> 
> That's my question: do we actually need that? Examples are welcomed.
> 
>  > "final" fills that purpose nicely.  If the value of the variable is 
> known at compile time, the compiler can constant-fold away the memory 
> access to that variable for a small speed boost.  If the value is only 
> determinable at runtime, that can still allow the compiler to make some 
> optimizations that would not be possible with mutable variables.
> 
> This is the part I'm not sure about. As with C++'s "const", I don't 
> think the compiler can conclude anything and therefor it can't optimize 
> anything. A "final" variable is not constant.

final int x = 42;

Change that :o).


Andrei



More information about the Digitalmars-d mailing list