Is it possible to add items to the arrays and hashes at compile time?

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jun 10 10:43:36 PDT 2015


On 06/10/2015 10:00 AM, Dennis Ritchie wrote:

> Is it possible somehow
> to create a more complex compilation process, which can reassign
> variables more than once?

I am not a compiler writer but I assume if a variable is not a 
compile-time expression, then the compiler generates code that makes it 
possible to initialize it at run time and modify it if mutable:

   int i = argc;
   // ...
   ++i;

On the other hand, if it's a manifest constant (enum, const static, 
etc.) then by definition it cannot be mutated. If we allowed mutation of 
compile-time expressions, then we would have a complicated language.

enum i = 42;
enum j = foo(i);    // Did foo() use 42 or 43?
i = 43;
enum k = foo(i);    // Did foo() use 42 or 43?

How can an enum value be changed? I find the above confusing.

The great thing about D's CTFE is that we can use arbitrarily complex 
expressions as long as they are available at compile time. For example, 
it is possible to make 'i' above a foreach loop variable and call foo() 
with different values.

What is your use case? I feel like it can be solved by other means.

Ali



More information about the Digitalmars-d-learn mailing list