program for building the program

Nick Sabalausky a at a.a
Tue Dec 1 07:46:47 PST 2009


"gzp" <galap at freemail.hu> wrote in message 
news:hf3dag$12b8$1 at digitalmars.com...
>
>> "Gzp" <galap at freemail.hu> wrote in message 
>> news:hf2k9a$2l54$1 at digitalmars.com...
>>> So to designing template(generic) code, a simplified language should be 
>>> created that generates the actual source. So the border b/n the two 
>>> language can be made more explicit and fewer questions arose.
>>>
>>
>> The problem with that, aside from the increase in the grammar's 
>> complexity, is that anytime you want to be able to do something at both 
>> runtime and compile-time, you'd have to write two separate 
>> implementations of the same thing, which carries with it all the problems 
>> assisiated with breaking DRY. Plus then that would create a need to write 
>> meta-meta-functions that generate both the runtime and compile-time 
>> versions of the same function. CTFE (and better yet, Nemerle's way, at 
>> least from what I've seen of it), is just a better approach.
>
> OK, CTFE is an optimization question for me. It is not the template, mixin 
> part of the language. This meta-programming let's you to generates codes 
> where you'd use cut/past/replace in a much safer, cleaner way.
> In CTFE you can give hints to the compiler: hey, please evaluate this 
> piece of code and substitute only the result. Like in Clean (fully 
> functional programming language) the actual calculation takes place in the 
> compiler as much as it can.
>

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);
}





More information about the Digitalmars-d mailing list