any news on const/invariant?

Oskar Linde oskar.lindeREM at OVEgmail.com
Thu Nov 29 06:01:27 PST 2007


Walter Bright wrote:
> Oskar Linde wrote:
>> Ok, but it is then unfortunate that having two constants:
>>
>> invariant a = 1;
>> const     b = 2;
>>
>> that typeof(&a) != typeof(&b).
> 
> It's not that bad. Consider:
>     int a = 1;
>     float b = 1;
> They're different types, too.

Oh, really? ;p. I don't think that is much of an argument. Think of the 
poor new user to D:

Q: How do I define constants?
A: Use invariant or const. Just pick one.

Q: Are they equivalent?
A: No, they result in distinct types.

Q: So which one should I really use?
A: Invariant is better. The references to the resulting type are then 
implicitly convertible to the other.

Q: So why is the other one called "const"?
A: ...

>> And regarding C++, it doesn't seem to me like const was originally 
>> intended to mean "read only view". I'd guess the logical evolution was 
>> 1. const as a storage class to replace #defines -> 2. pointers to 
>> const -> 3. contract const (I promise I wont change this). Should D 
>> really copy only the most unintended part of what const is from C++ 
>> and change the most intended one?
> 
> Is it really unintended? But even so, if you have invariant, you must 
> have const too, since everything is implicitly convertible to const, and 
> *that* is definitely intended in C++.

No, I can't say it was unintended. Rephrased, I will instead say that 
the "read only view" part of const is just one part of what const is in 
C++. The other part is the constant storage class that I've tried to 
argue is, in the case of constants at least, better handled by 
"invariant" in D. We can probably agree that the "const" keyword was 
chosen as an abbreviation for "constant", and as such, it is unfortunate 
that it now has lost the last connection to the meaning of that word.

>> I'm only saying that I think there is another set of keywords that 
>> have a much higher match between the dictionary meaning of the words 
>> and their semantic meaning in D.
> 
> I don't believe there is (and we looked!), and readonly isn't it. To me, 
> readonly means ROM (burned in to read only memory chips), not read only 
> view. I've even seen 'readonly' used in some languages to literally mean 
> "this stuff goes into ROM".

I know you are of that view, and your strongest association with the 
word "readonly" is in the sense of ROM, but I do believe you are in a 
minority. Uses of read-only in the context of access attributes, 
registers and views are well established and maps very well to the 
current "const" keyword. I expanded those arguments in:

http://www.digitalmars.com/d/archives/digitalmars/D/Const_sucks_58017.html#N58135
and
http://www.digitalmars.com/d/archives/digitalmars/D/D_const_design_rationale_54725.html#N54970

But I agree that "readonly" is far from a perfect keyword. The worst 
thing about it is that it is a contraction of two words (albeit a quite 
common one, yielding 3.5 million hits on google).

To summarize, I believe the arguments for the current keywords are:

* readonly is out of question
* following that, no better keywords have come up
* invariant was already a keyword
* const will be the most used form and therefore uses the most familiar 
keword
* const has a similar meaning in C++

The only reason I keep posting about this must be because I am an 
incurable optimist. Or maybe I am just too stubborn to realize I've 
lost. Or maybe I'm just stuck at the romantic idea that the const issue 
could be resolved in a way that leaves const meaning constant. :P

Another alternative to readonly could be to redefine the seldom used 
protection attribute "protected". Protected is perfect in many ways. A 
protected pointer or a protected slice of memory makes sense. It is a 
single word and has no confusing associations.

Const could then be redefined to mean "constant" again.
const -> protected
invariant -> const

The old protection attribute could be renamed:

private(class),

and at the same time, the package keyword could be removed and renamed

private(package),

with a net result of 1 less keyword than today, and no confusion about 
what keyword to use for defining constants.

-- 
Oskar



More information about the Digitalmars-d mailing list