On Tuesday, 18 December 2018 at 14:23:38 UTC, berni wrote:
import std.stdio;
class A
{
static immutable int[4] clue;
static this()
{
if(__ctfe) assert(0, "Yep, CTFE used");
foreach (i;0..4) clue[i] = i;
}
}
CTFE is used if and only if it MUST be used by context. That's a
runtime function, so no ctfe.
Do something like:
int[4] generate() {
int[4] tmp;
foreach(i; 0..4) tmp[i] = i;
return tmp;
}
static immutable int[4] clue = generate();
Since it is an immediate static immutable assign, ctfe MUST be
used, and thus will be used. With your code, you used a runtime
constructor on all runtime variables, so it did it at program
startup.
Generally:
static immutable something = something; // must be ctfe
enum something = something; // makes a ctfe literal (but not
necessarily static storage)
so those patterns force ctfe. While
static immutable something;
something = something; // NOT ctfe because it is a separate
statement!