Top 5

Don nospam at nospam.com.au
Thu Oct 9 07:10:06 PDT 2008


Steven Schveighoffer wrote:
> "Benji Smith" wrote
>> Gide Nwawudu wrote:
>>> 2) Finalise const/invariant stuff and change manifest const from
>>> 'enum' to 'define' (or whatever).
>>>
>>> define {
>>> double PI = 3.14;
>>> string author = "Walter";
>>> }
>>> define enum Direction { North, South, East, West };
>> I've never quite understood what people are talking about when they refer 
>> to a "manifest" constant. What does that mean?
>>
>> And why do we need any special keyword? What does the "define" keyword 
>> give you that an ordinary variable declaration doesn't? Why not just write 
>> the code from above like this:
>>
>>   double PI = 3.14;
>>   string author = "Walter";
>>   enum Direction { North, South, East, West };
>>
>> What am I missing here?
> 
> You cannot take the address of a manifest constant, and it doesn't live in a 
> static data space or in memory anywhere.  Instead it is built directly into 
> the code.
> 
> So when you say
> 
> define double PI = 3.14;
> 
> And then use PI:
> 
> auto x = PI;
> 
> This generates code that declares a variable x, then assigns it to 3.14. 
> the PI symbol isn't stored in the final code.
> 
> This has a huge benefit when you are declaring lots of constants, but only 
> few will be used.  You don't have to pay the penalty of storing all the 
> constants in your code, only the ones you use, and only where you use them.

Actually a smart linker would do that anyway. The only reason we need 
manifest constants is because OPTLINK isn't smart enough.
(And DMD isn't smart enough to discard unreachable variables from the 
symbol table).

const double PI = 3.14;

works exactly the same in D2 as it does in D1.
Except that you can't take the address of it in D1, but that's actually 
a bug. It's still stored. A perfect D compiler (D1 or D2) would make it 
identical to D2's
enum PI = 3.14;



More information about the Digitalmars-d mailing list