D's treatment of values versus side-effect free nullary functions

Jonathan M Davis jmdavisprog at gmail.com
Tue Jul 20 15:41:43 PDT 2010


On Tuesday, July 20, 2010 14:28:16 Justin Johansson wrote:
> Jonathan M Davis wrote:
> > Also, the
> > function could be used in CTFE to produce the value at compile time and
> > just use that value instead of calling the function at runtime.
> > Depending on how the function is used and how much optimizing the
> > compiler does, the function may never get called at runtime.
> 
> Thanks again.
> This bit of insight into CTFE was particularly useful to me. :-)

Glad to be of help. I would point out though that CTFE is its infancy at this 
point. I believe that the goal is to have the entirety of SafeD work with CTFE 
and likely use it in optimizations quite a bit. However, what can be used in 
CTFE is currently rather limited. You also generally have to force the issue a 
bit by using it in places where the value _must_ be known at compile-time (such 
as enum values, member variable initializations, etc.). I'm not sure that CTFE 
gets done at this point if it doesn't need to be. It's certainly a goal though 
for it to be much more expansive. And pure functions are a prime candidate for 
CTFE since they don't rely on global state at all (pure functions with no 
parameters even more so since there's never a case where you don't know one or 
more of their parameters at compile time).

So, how much CTFE does for you depends on the current state of the compiler (and 
likely whether you compiling with optimizations turned on), but it definitely has 
the capacity to run functions - especially pure ones - at compile time such that 
they may never get called at runtime.

- Jonathan M Davis


More information about the Digitalmars-d mailing list