Compile time function execution...
Andrei Alexandrescu (See Website For Email)
SeeWebsiteForEmail at erdani.org
Thu Feb 15 13:51:22 PST 2007
Walter Bright wrote:
> Michiel wrote:
>> That's a great feature! A couple of questions, if I may:
>>
>> * Can sqrt still call another function?
>
> Yes, as long as that function can also be compile time executed.
> Recursion works, too.
>
>> * Does it still work if classes or structs are involved in any way? What
>> about enums? What about arrays?
>
> No, yes, array literals only.
>
>> * Why is the second sqrt(10) run at runtime?
>
> Because the compiler cannot tell if it is a reasonable thing to try and
> execute it at compile time. To force it to happen at compile time:
>
> template eval(A...) { alias A eval; }
>
> writefln("%s", eval!(sqrt(10)));
>
> which works because templates can only take constants as value arguments.
The feature of binding expressions to aliases will break eval.
Andrei
More information about the Digitalmars-d
mailing list