2 ms·
Here's one of my favorite uses for it. I used to write a separate program to generate static tables. With compile time function execution, this was no longer ne
by WalterBright 2y ago
Here's one of my favorite uses for it. I used to write a separate program to generate static tables. With compile time function execution, this was no longer necessary. Here's an example:
__gshared uint[256] tytab = tytab_init;
extern (D) private enum tytab_init =
() {
uint[256] tab;
foreach (i; TXptr) { tab[i] |= TYFLptr; }
foreach (i; TXptr_nflat) { tab[i] |= TYFLptr; }
foreach (i; TXreal) { tab[i] |= TYFLreal; }
/* more lines removed for brevity */
return tab;
} ();
The initializer for the array `tytab` is returned by a lambda that computes the array and then returns it.
A link to the full glory of it:
https://github.com/dlang/dmd/blob/master/compiler/src/dmd/backend/var.d#L184 https://github.com/dlang/dmd/blob/master/compiler/src/dmd/ba...
Another common use for CTFE is to use it to create a DSL.