program for building the program

gzp galap at freemail.hu
Tue Dec 1 09:41:45 PST 2009


> 
> CTFE is used for optimization, but it is *also* used for metaprogramming:
> 
> char[] genDecl(char[][] names)
> {
>     char[] ret;
>     foreach(char[] name; names)
>         ret ~= "int "~name~";";
> 
>     return ret;
> }
> 
> void main()
> {
>     mixin( genDecl( ["foo"[], "a", "b"] ) );
>     a = 2;
>     b = 3;
>     foo = a + b;
>     assert(foo == 5);
> }
> 
> 

In this context it is a function to generate code and this code won't be 
ever called as a normal function. I've been thinking of functions where 
the code is invoked as a meta-programming code and as a normal function 
as well, but couldn't find any good example.

- some pre-calculated constants
(immutable)  const real pi = calculatePi();
(immutable)  const BigInt int = calculatePi( 100000 );

- a piece of code as in the example before

- I don't see too much use of codes like this:
int[ calcule_number_of_zero_position() ]  zero_positions;
They may provide some performance gain, but usually the size can be 
found out without complex ctfe. (Unless one is writing a compile time 
tracer)

Though I know there's no much chance that D2 is altered, but I think 
it'd help both the compiler (including complexity and speed issues), the 
language specification (simplifies) and the readability of the resulting 
sources if the two layers are distinct and not intermixed as now.



More information about the Digitalmars-d mailing list