Top 5

Sergey Gromov snake.scaly at gmail.com
Thu Oct 9 06:21:26 PDT 2008


Thu, 09 Oct 2008 09:07:19 -0400,
Benji Smith wrote:
> 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?

Your "PI" and "author" cannot be optimized because they're public and 
mutable, so every time you use PI in your code compiler must access a 
variable just in case some other module changed its value to 180.  Value 
of "North" on the other hand can never change so it can take part in 
constant folding etc.  You "manifest" an identity between name "North" 
and a number 0.

The closest to a manifest constant would be

invariant double PI = 3.14;
invariant string author = "Walter";

I think it even works in D2.  I don't know why enum were introduced for 
declaring constants.



More information about the Digitalmars-d mailing list