Content deleted Content added
Removed execessive references to the D language |
Added a second more meaningful example |
||
Line 41:
The use of <code>const</code> tells the compiler that the initializer for the variables must be computed at compile time<ref>[http://www.digitalmars.com/d/1.0/attribute.html#const D 1.0 language specification: Attributes]</ref>.
Such Compile-Time Function Evaluation (CTFE) can be used to populate simple data structures at compile-time in a simple way (D 1 code, the <code>~</code> operator concatenates arrays and items to arrays):
<source lang="D">
int[] genFactorials(int n) {
int[] result;
for (; n >= 0; --n)
result = factorial(n) ~ result;
return result;
}
const int N = 13;
static const auto factorials = cast(int[N])genFactorials(N - 1);
// Now 'factorials' contains at compile-time:
// [1, 1, 2, 6, 24, 120, 720, 5_040, 40_320, 362_880, 3_628_800,
// 39_916_800, 479_001_600]
</source>
== References ==
|