another idea for compile time functions

Knud Soerensen 4tuu4k002 at sneakemail.com
Thu Feb 8 21:29:01 PST 2007


On Thu, 08 Feb 2007 21:48:50 -0700, Hasan Aljudy wrote:

> The trick may lie in letting the compiler recognize these kind of 
> functions. A simple solution might me to come up with a new attribute; 
> let's call it "meta":
> # meta int add( int x, int y ) { return x + y; }
> This attribute will assist the compiler in recognizing that this 
> function can be computed at compile time if it's given constant arguments.
> 
> Now, this may not be very useful because of the restriction that meta 
> functions must not call other functions, which is very limiting; so we 
> need to extend it.
> 
> If a function takes constant arguments and does call other functions, 
> then it can only be executed at compile time if all of these other 
> functions are meta functions.
> 

I don't think a meta attribute is need, 
if the compiler detects that a function is called with only 
constant arguments it should try to inline the function and do constant
folding on it.
If the function calls another function then it should check if constant
folding reduce the arguments to constant and then try to inline and
folding it, if this fails it should just call the function.

If a signal to the compiler is really needed then maybe ! can be used

add!(2,3) would make the compiler look for the template "add!" first 
and then try to fold "add" if the template is not found.



More information about the Digitalmars-d mailing list