D const design rationale

James Dennett jdennett at acm.org
Fri Jun 22 22:52:30 PDT 2007


Walter Bright wrote:
> Sean Kelly wrote:
>> Walter Bright wrote:
>>> In C++, sometimes const means invariant, and sometimes it means
>>> readonly view. I've found even C++ experts who don't know how it works.
>> Odd.  The C++ system always seemed extremely simple to me.
> 
> It isn't. 

It is to many people; you seem to have more experience with
people who have trouble understanding it, maybe.  I don't
know why else it is that I find your diatribes about the
terrible problems of C++'s const to be so strange, given
that C++'s const works widely and well.

> I run into people all the time who are amazed to discover that
> const references can change. 

In C++ code without undefined behavior, references cannot
change.

Maybe you're referring to the fact that some people don't
learn that const means read-only in C++, rather than the
"invariant" notion that you've introduced for D 2.0.  That
seems not to be hard for competent programmers to learn,
compared to many of the other things they have to learn.

(I'm concerned at using two synonyms to mean different
things, but it's almost certainly too late to change
that now.)

> Few understand when const is invariant and
> when it isn't. I've never even seen anyone mention the problem where the
> non-transitive const destroys any hope of having FP like capabilities in
> C++.

And yet C++ *has* "FP-like" capabilities, depending on how
we define our terms.  Const works well in the hands of many,
many C++ programmers.  Bad programmers will be bad programmers
with D too.

Value, rather than reference, semantics are a great aid in
providing FP capabilities in a manner that scales to parallel
systems.  I'm glad to see that D is getting closer to C++ in
providing more capable structs.

>> I personally find the use of three keywords to represent three
>> overlapping facets of const behavior to be very confusing, and am
>> concerned about trying to explain it to novice programmers.  With
>> three keywords, there are six possible combinations:
>>
>> final
>> const invariant
>> final const
>> final invariant
>> const invariant
>> final const invariant
> 
> Probably the thing to do is simply outlaw using more than one.

That would simplify things, I think.

-- James



More information about the Digitalmars-d mailing list