Top 5
Steven Schveighoffer
schveiguy at yahoo.com
Thu Oct 9 06:23:12 PDT 2008
"Sergey Gromov" wrote
> 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.
You can still take the address of invariant variables, and since the
compiler never knows who will use them, it will reserve space for them in
your executable, even if they aren't used.
-Steve
More information about the Digitalmars-d
mailing list