Motivation for compile time function execution

Sean Kelly sean at f4.ca
Thu Feb 15 21:25:58 PST 2007


Kevin Bealer wrote:
> 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);
> }

I think the above will actually be folded to a constant value at compile 
time, assuming sqrt is defined properly?


Sean



More information about the Digitalmars-d mailing list