xxxxxxxxxx
import std.stdio;
bool isRunTime()
{
return !__ctfe;
}
bool isCompileTime()
{
static if (!isRunTime())
return __ctfe;
else
return false;
}
string onlyCompileTime()
{
static assert(isCompileTime(),
"This function can only be executed at compile time");
assert(isCompileTime(),
"This function can only be execute at compile time");
return "onlyCompileTime";
}
string onlyRunTime()
{
// This assert will actually throw an error at compile-time,
// if an attempt is made to execute it at compile time.
assert(isRunTime(), "This function can only be executed at run time");
return "onlyRunTime";
}
void main(string[] args)
{
static assert(isCompileTime());
static assert (!isRunTime());
assert(!isCompileTime());
assert(isRunTime());
static assert(onlyCompileTime() == "onlyCompileTime");
// Compile-time Error: Good!
//static assert(onlyRunTime() == "onlyRunTime");
assert(onlyRunTime() == "onlyRunTime");
// Compile-time Error: Good!
// pragma(msg, onlyRunTime());
// I couldn't figure out how to force `onlyCompileTime` to
// execute at runtime. That's probably a good thing.
}