'final' variables
Lionello Lunesu
lio at lunesu.remove.com
Tue Mar 20 09:17:36 PDT 2007
Andrei Alexandrescu (See Website For Email) wrote:
> 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)
> {
> ...
> }
I understand, but what's the point of telling the compiler "I don't want
to rebind this variable"? It doesn't seem like an optimization hint.
Like telling "func(const int i)" in C++ doesn't actually do anything.
You just type more. And if you later want to rebind 'i' you'll have to
change the interface. Lose-lose?
L.
More information about the Digitalmars-d
mailing list