'final' variables

Lionello Lunesu lio at lunesu.remove.com
Tue Mar 20 09:21:28 PDT 2007


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.

L.



More information about the Digitalmars-d mailing list