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

Dennis Ritchie via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jun 9 21:36:34 PDT 2015


On Wednesday, 10 June 2015 at 03:38:32 UTC, Ali Çehreli wrote:
> 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:

Thanks. It turns out I can do this:

import std.stdio;

auto merge(Hashes)(Hashes[] hashes...) {

     int[][int][int] result;

     foreach (hash; hashes) {
         foreach (key, value; hash) {
             result[key] = value;
         }
     }

     return result;
}

enum firstPart = [1 : [ 1 : [1, 1] ] ];
enum secondPart = [2 : [ 2 : [2, 2] ] ];

int[][int][int] init_ctHash(int i) {

     auto result = merge(firstPart, secondPart);

     result[i] = [ i : [i, i] ];

     return result;
}

void main() {

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

     enum t = merge(ctHash, init_ctHash(6));

     writeln(t);
}

But I can not do so:

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

ctHash = merge(ctHash, init_ctHash(6));

I have a question: why variables may not be initialized more than 
once? Why can't they to resave at compile time?


More information about the Digitalmars-d-learn mailing list