Top 5

Bill Baxter wbaxter at gmail.com
Thu Oct 9 06:27:13 PDT 2008


On Thu, Oct 9, 2008 at 10:21 PM, Sergey Gromov <snake.scaly at gmail.com> 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.
>

I think the main (only?) difference is that you can take the address
of an invariant constant, but not a manifest constant.

--bb



More information about the Digitalmars-d mailing list