const, final, scope function parameters

James Dennett jdennett at acm.org
Sun May 27 20:34:21 PDT 2007


Myron Alexander wrote:
> James Dennett wrote:
>> Walter Bright wrote:
>>> 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).
>>
> 
> James,
> 
> Having const default for all variable declarations would be problematic
> in that most variables are expected to be mutable. The whole point of
> variables is that they are variable :)

I disagree!  Experience (from C++) has shown me that a
large proportion of "variables" are not changed after
their initialization.  I find that it's true of most
local variables (including function parameters), and
using immutable objects with reference semantics is
also fairly common.

The advantages in compiler checking mean that even if
there is a (sufficiently small) additional cost in
having the default be immutability, my experience
strongly suggests that it would be worthwhile.  As
I've said, I also find that the cost would be small,
given how many "variables" aren't variable in clean
code.  (Who knows, maybe this default could even
encourage people to use new variables when they have
a different value with a different meaning, rather
than reusing a "convenient" local variable which was
used for something else.  But now I'm dreaming.)

> I think the one exception is strings in that the D implementation is an
> array but conceptually, there is a difference of opinion for whether
> strings are buffers or values. I am firmly on the "string is a value"
> bench and have my wet trout ready to go slap the "other" side when the
> mother of all wars begins :) (Actually, I have a nuke-lee-are trout that
> is const, non-static, and very final ;))

My understanding is that in D, arrays (almost) have
reference semantics.  It's a shame, IMO, but languages
have to make choices and there's no way one language
is going to make them all the way I would unless I
design it myself (and I don't believe that I could
successfully bring a language to a mass audience as
Walter is attempting to do).

-- James



More information about the Digitalmars-d mailing list