Jump to page: 1 2 3
Thread overview
March 20, 2007
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.
March 20, 2007
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, we need a new keyword for non-mutable variables.  "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.
March 20, 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, we need a new keyword for non-mutable variables.
<snip>

C++ manages with const for both, so why can't D?  What circumstance is there in which either keyword would be valid with different meanings?

Stewart.
March 20, 2007
Stewart Gordon 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, we need a new keyword for non-mutable variables.
> <snip>
> 
> C++ manages with const for both, so why can't D?
> What circumstance is there in which either keyword would be valid
> with different meanings?

Some D reference types do not have the pointer qualifier, so this C++ declaration:

    int const * const x;

could not be directly reproduced in D, except for maybe:

    int const [] const x;

which is horrifying :-)  And it still doesn't address the issue of class references.


Sean
March 20, 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
March 20, 2007
Stewart Gordon 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, we need a new keyword for non-mutable variables.
> <snip>
> 
> C++ manages with const for both, so why can't D?  What circumstance is there in which either keyword would be valid with different meanings?

If you apply "const" to a class reference, how would you determine which of {reference,object} the const applies to?

- If to the reference only, you can't express the constness of objects.
- If to the object only, you can't express non-rebindability of the reference. (C++ doesn't have this problem, since the only references it has are implicitly final all by themselves)
- If to both, you can only express constness of objects by simultaneously disallowing the reference to be rebound. Not nice.

I think that covers all cases (except 'neither', but that simply doesn't make any sense ;) ).
March 20, 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.
March 20, 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.
March 20, 2007
Sean Kelly wrote:
> Stewart Gordon 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, we need a new keyword for non-mutable variables.
>> <snip>
>>
>> C++ manages with const for both, so why can't D?
>  > What circumstance is there in which either keyword would be valid
>  > with different meanings?
> 
> Some D reference types do not have the pointer qualifier, so this C++ declaration:
> 
>     int const * const x;
> 
> could not be directly reproduced in D, except for maybe:
> 
>     int const [] const x;
> 
> which is horrifying :-)  And it still doesn't address the issue of class references.

I _swear_ I hadn't seen this post when posted one with the same syntax in it :o).

Andrei
March 20, 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
« First   ‹ Prev
1 2 3