Compile-time function execution: Difference between revisions

Content deleted Content added
Updated D example
Example: Added detail
Line 26:
</source>
 
But withUsing compile time function evaluation the, code used to compute the factorial would be exactly the same as what one would write for run time evaluation.
Here's (thisan example codeof isCTFE in the [[D programming language]]<ref>[http://www.digitalmars.com/d/1.0/function.html#interpretation D 1.0 language specification: Functions]</ref>):
 
<source lang="D">
Line 40 ⟶ 41:
</source>
 
This example specifies a valid D function called "factorial" which would typically be evaluated at run time. The use of <code>const</code> tells the compiler that the initializer for the variables must be computed at compile time. Note that the arguments to the function must be constant as well.<ref>[http://www.digitalmars.com/d/1.0/attribute.html#const D 1.0 language specification: Attributes]</ref>
 
CTFE can be used to populate data structures at compile-time in a simple way (D version 2):