Final, Const, Invariant

Walter Bright newshound at digitalmars.com
Mon Mar 26 03:54:54 PDT 2007


Don Clugston wrote:
> Walter Bright wrote:
>> Don Clugston wrote:
>>> My code has const everywhere. In fact, I use 'const' about ten times 
>>> as often as 'static' or 'class'. And the new scheme does not seem to 
>>> have a direct equivalent for it. How can I say, "I want to refer this 
>>> literal by a name, but in all other respects I want it to behave 
>>> exactly as a literal"? In particular, I do not want storage 
>>> associated with it (attempting to take its address is a bug), and it 
>>> should work with CTFE.
>>
>> Replacing const with final should do the trick.
> 
> Will that prevent taking the address, the same way 'const' does now?

It's ironic, the "can't take the address of a const" is actually a bug. 
I guess it has inadvertently turned into a feature! I could argue it 
either way.


> There's also this previously discussed case:
> 
> void func(int x)
> {
>   final a = ctfe_func(7);  // will never change, ever.

Make it static final. There's no reason to put such a declaration on the 
stack.

>   final b = non_ctfe_func(8); // won't change during this function call

Leave as is.

> }
> 
> A largely unrelated question, why are existing 'const' items stored in 
> the obj file (instead of being discarded immediately after use)?

Because it's a bug that one cannot take the address of them. But it 
could become a feature, in which case they shouldn't be put in the obj file.

> (I'm 
> excluding uninitialised class 'const' items set in the constructor, 
> which are fundamentally different).



More information about the Digitalmars-d mailing list