so what exactly is const supposed to mean?

Don Clugston dac at nospam.com.au
Tue Jul 4 04:27:45 PDT 2006


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.




More information about the Digitalmars-d-learn mailing list