any news on const/invariant?

Oskar Linde oskar.lindeREM at OVEgmail.com
Thu Nov 29 00:52:59 PST 2007


Walter Bright wrote:
> Oskar Linde wrote:
>> Walter Bright wrote:
>>
>>> Because those alternatives all look terrible. And frankly, how could 
>>> anyone be confused about what:
>>>     const x = 3;
>>> means? I can't understand throwing that out to improve clarity?
>>
>> Honestly, I am confused about what that means. If I define an integer 
>> constant x, don't I want a pointer to that constant to be 
>> invariant(int)*? Doesn't that mean that constants should rather be 
>> defined as:
>>
>> invariant x = 3;
>>
>> Or am I wrong? What am I missing here?
> 
> For a basic type, the meaning of const and invariant overlap. The 
> difference between them is when you have pointers or other references. 
> For simple integers, you can use const or invariant.

Ok, but it is then unfortunate that having two constants:

invariant a = 1;
const     b = 2;

that typeof(&a) != typeof(&b). And that given those types, only one is 
convertible to the other. Since invariant(int)* is convertible to 
const(int)*, it seems to me that there is a good reason to define 
constants using invariant, rather than const.

invariant(int)* also captures the full meaning of a pointer to constant 
data.

>> I am to tired right now to reiterate my full thoughts on the keywords 
>> (and I am sure you are all very thankful for that :) ), but it still 
>> feels like the keywords are reversed:
>>
>> invariant === constant
>>     const === read only
> 
> We went around this for a while. There is no word that unambiguously 
> means "read only view" while another word meaning unambiguously "will 
> never change". So, since C++ uses const to mean "read only view", it 
> seemed best to leave that as it was.

Thank you for this motivation. Regarding describing words, in my book 
the word "constant" pretty well describes something that "will never 
change". And just like:

float means "floating point number"
int means "integer"

const could mean "constant"
readonly could mean "read only view"

It wouldn't be hard to explain or motivate. 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?

> readonly, constant, invariant, immutable, are all synonyms.

Only for a very weak definition of synonyms, i.e. "has overlapping 
meanings". That doesn't mean that they can (or should) be used 
interchangeably. For example, you can say that secure and protected are 
synonyms, but if you went to climb a cliff and told people you had 
"protected the rope", I bet at least a few of them would be confused. :)

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.

-- 
Oskar



More information about the Digitalmars-d mailing list