Compile time function execution...

Bill Baxter dnewsgroup at billbaxter.com
Thu Feb 15 13:38:41 PST 2007


Michiel wrote:
> Frits van Bommel wrote:
> 
>>> * Why is the second sqrt(10) run at runtime? In theory it's still a
>>> constant, right? Is this something that will work in a later version?
>> From the spec:
>> """
>> In order to be executed at compile time, the function must appear in a
>> context where it must be so executed, for example:
>>
>>     * initialization of a static variable
>>     * dimension of a static array
>>     * argument for a template value parameter
>> """
>>
>> So this is only done if it's used in a context where only compile-time
>> constants are allowed.
> 
> Well, then there is room for improvement. (Good thing, too. Can you
> imagine how bad it would be if perfection had already been achieved? ;))
> 
> Anyway, it looks to me like every subtree of the abstract syntax tree
> could potentially be collapsed by compile time function execution. This
> would include the mentioned sqrt call. Of course, I don't really know
> how the D compiler works internally, so I can't be sure in this case.
> 
> And I don't see a real reason why structs and at least scope classes
> couldn't be included. But I don't suppose it's all that easy.
> 
> Maybe the perfect compiler would also pre-execute everything up until
> the first input is needed. And maybe some bits after.

You do need some way to turn it off though.
For an extreme example, most programs from the demoscene make extensive 
use of compressed data that is uncompressed as the first step before 
running.  They would be very unhappy if their language chose to be 
"helpful" by running the decompression routines at compile-time thus 
resulting in a 20M executable.

Extreme -- but it does demonstrate there are cases where you want to be 
sure some expansion happens at runtime.

--bb



More information about the Digitalmars-d mailing list