D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 3556 - version(CTFE)
Summary: version(CTFE)
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D1 (retired)
Hardware: Other All
: P2 enhancement
Assignee: No Owner
URL:
Keywords: patch
Depends on:
Blocks: 1924 2066
  Show dependency treegraph
 
Reported: 2009-11-28 18:04 UTC by Nick Sabalausky
Modified: 2014-04-18 09:12 UTC (History)
5 users (show)

See Also:


Attachments
Patch against DMD2 svn 324 (1.81 KB, patch)
2010-01-03 14:30 UTC, Don
Details | Diff

Note You need to log in before you can comment on or make changes to this issue.
Description Nick Sabalausky 2009-11-28 18:04:32 UTC
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.
}
Comment 1 Don 2010-01-03 14:30:41 UTC
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.
Comment 2 Walter Bright 2010-01-11 22:05:10 UTC
Changeset 332
Comment 3 Walter Bright 2010-01-11 22:06:43 UTC
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.
Comment 4 Leandro Lucarella 2010-01-13 06:21:22 UTC
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.
Comment 5 Lars T. Kyllingstad 2010-01-13 06:35:28 UTC
How about

  if (meta.inCTFE) { ... }

for instance? (Assuming that __traits(xxx) becomes meta.xxx, of course.)
Comment 6 Don 2010-01-13 06:55:16 UTC
(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;
-----
Comment 7 Walter Bright 2010-01-30 22:45:33 UTC
fixed dmd 2.040
Comment 8 Stewart Gordon 2010-01-31 06:16:49 UTC
(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.