const, final, scope function parameters

Reiner Pope some at address.com
Sun May 27 14:43:12 PDT 2007


Bill Baxter wrote:
> Regan Heath wrote:
>> Walter Bright Wrote:
>>> Frank Benoit wrote:
>>>> Perhaps we look at it from the wrong side.
>>>>
>>>> If we want to change the D language to make it more const, the keywords
>>>> 'const', 'invariant'... are probably the wrong choice.
>>>>
>>>> How about restricting keywords and add their opposites: 'mutable',
>>>> 'once' (write once) and then make every variable declaration const by
>>>> default? Each variable/parameter needs to be made modifyable with
>>>> modifiers if needed.
>>> I think having to write:
>>>
>>>     mutable int x;
>>>
>>> instead of:
>>>
>>>     int x;
>>>
>>> just isn't going to please people.
>>
>> Ahhh, I think I see what you're concerned about.  As in this example?
>>
>> mutable int gx;
>> void foo(int y) {  //y is scope const final
>>   mutable int z;
>> }
>>
>> where the global and local scope ints 'gx' and 'z' are not supposed to 
>> be const scope final.
>>
>> Why can't we apply 'scope const final' to function parameters only?
> 
> Because he said "that seems to throw people for a loop."  I'm guessing 
> the qualm is that in this:
> 
> int* gx;
> void foo(int* y) {
>   int* z;
> }
> 
> all three of those declarations sure look the same to an uninitiated C++ 
> (or current D) user.  So I think by "throws people for a loop", Walter 
> means "isn't obvious to C++ converts".  But well... all I have to say to 
> that is why would you expect any form of const by default to look 
> natural to a C++ user?  It's just not going to.  But the hope is they'll 
> thank you for it in the long run.
> 
> --bb
> 
I don't think it's a rule you're likely to forget, either, because it 
makes sense and can be concisely phrased: "because the safest and most 
common way to deal with function parameters is by not modifying or 
retaining them outside the function scope, function parameters default 
to 'const scope final'"

Also, const won't affect the behaviour of your code, so the only place 
you will run into difficulties is when trying to compile bad code. In 
that case, we hope the compiler can give a nice error message. :)


   -- Reiner



More information about the Digitalmars-d mailing list