'final' variables

Andrei Alexandrescu (See Website For Email) SeeWebsiteForEmail at erdani.org
Tue Mar 20 08:50:44 PDT 2007


Lionello Lunesu wrote:
> I've been trying to follow the thread on the const/final/invariant 
> extensions but I have to wonder: how useful are final variables?
> 
> final int i = 2;
> 
> This would mean: 'i' cannot be rebound to another value. Right? So this 
> final is actually C++'s const.
> 
> void func( final int i )
> {
>   //i is not const but cannot be set either
> }
> 
> Also here, it's like C++'s const, but with the _removed_ benefit of 
> restricting the accepted types. Is this correct?
> 
> What's the use of "final" for variables? I'm saying "for variables" 
> because for methods the benefit is only too clear.
> 
> L.
> 
> PS. I've started a new thread because this isn't a reply to any 
> particular post.

Very simple.

void Fun(const char[] s)
{
   ...
}

Cannot modify s's content, but can rebind it. If the programmer wants to 
not rebind, then she writes:

void Fun(final const char[] s)
{
   ...
}


Andrei



More information about the Digitalmars-d mailing list