On Wed, Aug 31, 2016 at 10:43 PM, Stefan Koch via Digitalmars-d-announce <digitalmars-d-announce@puremagic.com> wrote:
On Tuesday, 30 August 2016 at 22:01:45 UTC, tsbockman wrote:
On Monday, 29 August 2016 at 08:39:56 UTC, Stefan Koch wrote:
I just came up with a nifty little patch that makes it possible to ensure that a function is _only_ used at ctfe.
Or the opposite.

static assert(__ctfe, "This function is not supposed to be called outside of ctfe");
and static assert(!__ctfe, "This function is not supposed to be called during ctfe");

similarly you can use static if (__ctfe).

Is it worth trying to get it into master ?

Yes, please. I've often wished I could use `__ctfe` in a `static if`.

Sorry. It I overlooked a something rather important. static __ctfe is currently not possible and it's rather expensive to make it possible.


Surely changing the current implementation slightly could still work if we made a library implementation like:

========
// ? module std.exception.enforce_ctfe;

void main() {
ctfefunc();
}


string ctfefunc() {
static if (assertCTFE!true) {
throw new Exception("asdf");
}
return `import std.stdio; writeln("ctfe generated this");`;
}

template assertCTFE(bool b) {
enum assertCTFE = __traits(compiles, _checkCTFE());
}
void _checkCTFE() {
import std.uuid;
enum id = new UUID("8AB3060E-2cba-4f23-b74c-b52db3bdfb46");
}


the _checkCTFE() function is just a function that does something we're not allowed to do at CTFE, but current implementation does not respect __traits(compiles, ....);



As far as I can tell that is a bug. Thoughts?