Until CTFE includes all features of runtime and has 100% same semantics as runtime (and maybe even after then), there is often a need to have separate codepaths for ctfe and runtime. A version(CTFE) would be an improvement over the current solution of making alternate CTFE-intended functions and using a naming convention to distinguish. version(CTFE) { // Sacrifice speed, flexibility, or anything else // necessary to make this code work via CTFE. } else { // Use proper D. }
Created attachment 542 [details] Patch against DMD2 svn 324 Here is a patch which adds __ctfe as a 'magic variable'. It's a bool which is true if evaluated in a function at compile-time, but false at run-time. If evaluated outside a function, it gives a "non-constant expression" error. Simple example: --------- import std.stdio; int foo() { if (__ctfe) return 3; writefln("run-time evaluated"); return 2; } static assert(foo()==3); void main() { assert(foo()==2); } -------- The back-end throws out the if(__ctfe){...} part (even without optimisations turned on), so there is no runtime penalty for adding if(__ctfe) clauses.
Changeset 332
The reason to prefer if(__ctfe) over version(CTFE) is that both code paths need to be compiled properly, as a function could be executed both at compile and runtime.
Being that D2 is close to finalization, why do you keep introducing language identifiers starting with __. I think it's a very bad idea, as they are historically used for implementation specific features.
How about if (meta.inCTFE) { ... } for instance? (Assuming that __traits(xxx) becomes meta.xxx, of course.)
(In reply to comment #4) > Being that D2 is close to finalization, why do you keep introducing language > identifiers starting with __. I think it's a very bad idea, as they are > historically used for implementation specific features. This *is* an implementation-specific feature. The user interface is not yet determined. Note that you can already write: ----- module meta; alias __ctfe inCTFE; -----
fixed dmd 2.040
(In reply to comment #4) > Being that D2 is close to finalization, What are you talking about? Not even D1 is finished yet, therefore what you say is impossible.