Top 5
Benji Smith
dlanguage at benjismith.net
Thu Oct 9 06:27:04 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.
Oops. Silly me. I meant to declare those variables as const (or
invariant, like in your example).
I understand the optimization benefits of const values. I'v just never
understood what people are talking about with "manifest" constants, and
why they deserve to have some special keyword.
I take it, from your example code, that you agree with me that there's
no need for a "define" or "manifest" keyword...
--benji
More information about the Digitalmars-d
mailing list