so what exactly is const supposed to mean?

Chris Nicholson-Sauls ibisbasenji at gmail.com
Tue Jul 4 05:03:53 PDT 2006


Don Clugston wrote:
> Bruno Medeiros wrote:
> 
>> D's const has two meanings. The usual meaning is from the form:
>>   const int var = <some constant initializer>;
>> and it means that the variable is a compile-time constant and no 
>> storage is allocated for it, instead the value is substituted whenever 
>> the var is used (like #define, but safer).
>>
>> The second meaning is from the form:
>>   const int var; // no initializer
>> and means that the var must be initialized once in a constructor, and 
>> then it becomes non-recursively immutable (meaning you can't change 
>> the value of the var, but you can change referenced values). The var 
>> has storage and as such is an lvalue.
>> This is basically the same as final in other languages (Java, C#), and 
>> I wonder why it isn't in D as well(?). Walter said he didn't want a 
>> const keyword overloaded with many different meanings.
> 
> 
> I agree. I think it's a mistake to have a 'const' that isn't actually 
> const! 'final' seems far more appropriate.
> 

We do have a 'final' keyword in D already.  Applied to a class decleration it describes a 
class which may not be inherited from.  Applied to class member function (method) 
declerations, it means a member which may not be overriden.  So far as I am aware, it is 
meaningless anywhere else.  Since 'final' in D currently means, in over-generalized terms, 
"this cannot be replaced," I don't think it would be much of a stretch to use 'final' to 
mean what 'const' currently does.

-- Chris Nicholson-Sauls



More information about the Digitalmars-d-learn mailing list