Memoization in compile-time

Dennis Ritchie via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Mar 13 12:09:35 PDT 2015


On Friday, 13 March 2015 at 18:38:16 UTC, Ali Çehreli wrote:
> 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

Thanks.

And you can make the same memoized variable was available at 
compile time and at run time? :)


More information about the Digitalmars-d-learn mailing list