D's treatment of values versus side-effect free nullary functions

bearophile bearophileHUGS at lycos.com
Tue Jul 20 17:24:33 PDT 2010


Jonathan M Davis:
> But theoretically, it 
> could be used for optimizations just like inlining (particularly with pure 
> functions), and that being the case, it probably will at some point.

Pure functions go well with CTFE because they don't use global state, but you can run code at compile time only if all the arguments are known at compile time :-)

If some of their arguments are known at compile time and some of them are not known, and you want to optimize better, then you need partial compilation, that is a bit hard to implement, probably makes the compiler slow and complex.

So for a practical form of manual partial compilation practical time ago I have proposed something similar to GCC __builtin_constant_p (http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Other-Builtins.html#index-g_t_005f_005fbuiltin_005fconstant_005fp-2392 ), that is syntax sugar that you can use inside functions to turn them into templates (I don't know if you can also have a single entry point to keep the possibility of taking their address). You can use a static if with such __trait(isCTAgument, foo), to tell if each function argument as foo is known at compile time, and do something with it if it is. This is like defining those arguments as CT template arguments, that later the optimizer can optimize better (and perform the partial compilation).

Bye,
bearophile


More information about the Digitalmars-d mailing list