Extended Type Design.
Frits van Bommel
fvbommel at REMwOVExCAPSs.nl
Fri Mar 16 10:58:09 PDT 2007
Andrei Alexandrescu (See Website For Email) wrote:
> Don Clugston wrote:
>> My problem is, I don't see any connection between "don't touch" and
>> "constant" except that C++ got them confused. In every other other
>> context outside of C++, "constant" means "it will not change".
>>
>> Mathematically, you can "hold something constant" (even though it is
>> actually a variable), but as long as it is being held constant, it
>> can't change.
>>
>> Suppose in a function, you have 'const' access to a global variable.
>> Another thread with write-access to that variable can change it, while
>> the function is executing.
>>
>> It makes sense for a function to have read-only access to a variable,
>> and need to obtain a mutex when it wants to obtain the current value.
>> But that's not a constant in any reasonable sense of the word.
>
> The difference between math and computer memory is that all too often in
> the latter case you want to express modular separation very often.
> Modular separation (in this context) means that the right to modify a
> certain object is owned by some part of program, and other parts may be
> offered only a view of that memory. This is such an important concept,
> even C adopted it. All of C's library functions that take pointers are
> anontated in their signature to specify whether or not they modify their
> input or not.
>
> You want to enforce modular mutability cheaply. If D's immutability
> features fail to achieve that, we've done nothing.
I don't think he's arguing against immutable views, just against calling
it 'const' ;).
In his words, from an earlier post:
=====
Don Clugston wrote:
> IMHO (b) should be 'readonly' and (c) should be 'const'.
> But this may be because I'm a physicist, and for me a constant is
> something like the speed of light, and C++'s const_cast always seemed
> utterly nonsensical.
>
> void alabama() { // Actually compiles! Someone should be shot for this.
> const double PI = 3.14159265358;
> *const_cast<double *>(&PI) = 4.0;
> }
>
> Whereas 'readonly' seems to be a much better match for (b). Although
> it's an extra keyword, it seems to me that the concept discussed here is
> sufficiently fundamental to justify an extra keyword. Especially if we
> have a chance to rid of 'lazy'.
=====
In fact, I think most people here will agree that the ability for a
function to specify "I won't modify this" enforced by the compiler
(modulo pointer casts etc.) is a good thing. Especially if, as you
mentioned, the function can return an (im)mutable result depending on
whether a parameter was (im)mutable without having to rewrite the
function, since that seems to be the biggest annoyance about C++'s
constness system.
More information about the Digitalmars-d
mailing list