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
Tue Jun 9 20:38:26 PDT 2015


On 06/09/2015 06:53 PM, Dennis Ritchie wrote:

 >      ctHash[4][4] ~= [4, 4]; // I want this to work at compile time :)
 >      // Possible?

It is possible but not exactly as you wrote. In other words, it is not 
as flexible.

The way I understand it and the way it makes sense to me, :) variables 
that are generated at compile time can be initialized only once. It is 
not possible after initialization. However, the initialization of the 
variable can be as complex as needed:

enum int[][int][int] ctHash = init_ctHash();

int[][int][int] init_ctHash(){
     auto result = merge(firstPart, secondPart);
     result[4] = [4 : [4, 4 ]];
     return result;
}

(I couldn't get ctHash[4][4] to work; so I changed the code like above.)

Ali



More information about the Digitalmars-d-learn mailing list