Top 5

Benji Smith dlanguage at benjismith.net
Thu Oct 9 06:37:07 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.
> 
> -Steve 

Okay. That makes sense.

I was just working with the tango.sys.win32.Types module yesterday, and 
it uses "enum" to declare something like five or six thousand different 
named constants, so I can see where that kind of thing would be helpful.

--benji



More information about the Digitalmars-d mailing list