Motivation for compile time function execution

Kevin Bealer kevinbealer at gmail.com
Thu Feb 15 21:00:45 PST 2007


Walter Bright wrote:
> Sean Kelly wrote:
>> I think this is a fantastic feature.  It uses plain old D syntax and 
>> offers all the computational power of templates.  Will this work for 
>> template functions that follow the rules as well?
> 
> Yes.
> 
>> Also, is there any way to test whether a function is being executed at 
>> run time?
> 
> Yes, it will be always executed at runtime if it is not in a context 
> where it is required to be folded to a constant.

This looks awesome, by the way!

I don't have this version of dmd yet, but it looks like I can convert 
any expression to be run at compile time or runtime with not too much 
extra syntax.

int hack(int a, int b)
{
    return sqrt(a) + b*b;
}

int heck()
{
    // at runtime
    return hack(10, 20);
}

int hick()
{
     // compile time
     mixin("return "~ToString!(hack(11,22))~";");
}

Kevin



More information about the Digitalmars-d mailing list