Memoization in compile-time

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Mar 13 11:38:14 PDT 2015


On 03/13/2015 11:28 AM, Ali Çehreli wrote:

 > enum int[] factorials = memoizeFactorials(N);

Oops! That's generally a trap! The array better be 'static' because a 
manifest constant like 'enum factorials' would be inserted everywhere it 
is used. (Similar to a C macro.)

I've been scratching my head why the assertion below was failing.

It sould be 'static':

static int[] factorials = memoizeFactorials(N);

If it's an enum, the following assert will fail:

     foreach (i; 0 .. N) {
         assert(factorials.ptr + i == &(factorials[i]));
     }

Make it a 'static', it will pass because then there will be just one 
factorials array.

Ali



More information about the Digitalmars-d-learn mailing list