const, final, scope function parameters

James Dennett jdennett at acm.org
Sun May 27 12:59:33 PDT 2007


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.

Maybe not.  It would please me; in C++ right now, I usually
have to write "int const x = ...;" whereas if the default
were the "safe" form I could write just "int x = ...;".
(As usual, "int" is just an example, of course.)

Writing

  var int x;

would be just fine by me; I find it more readable than
using "mutable" (and the C++ community already has a
similar-but-different meaning for mutable, as you know,
so using a different term might be helpful).

It's often been said that if C++ were being designed from
a clean start that const would be the default.  D has had
that clean start -- and has made various changes that C++
would make but cannot for backwards compatibility reasons.
It would be nice to make some more steps in the right
direction while D still has a _relatively_ small existing
user base and code base.

(One direction in which C++ and D are going in different
directions is default definitions for special member
functions; most of those involved in C++ would seemingly
like fewer defined by default, whereas if I remember, D
tends to define more, such as memberwise comparison.  C++
defined those that it has implicitly largely for C
compatibility.  The best option, currently under discussion
for C++, seems to be to allow users to explicitly request
that normal forms of certain operations be provided or
excluded.  But I digress.)

-- James



More information about the Digitalmars-d mailing list