'final' variables
Andrei Alexandrescu (See Website For Email)
SeeWebsiteForEmail at erdani.org
Tue Mar 20 09:29:52 PDT 2007
Lionello Lunesu wrote:
> 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?
Many people, including yours truly, appreciate the ability of
automatically checking their assumptions or desires.
Andrei
More information about the Digitalmars-d
mailing list