Get body of a function as string

monarch_dodra monarchdodra at gmail.com
Fri Jun 28 14:21:15 PDT 2013


On Friday, 28 June 2013 at 20:50:55 UTC, John Colvin wrote:
> On Friday, 28 June 2013 at 13:55:54 UTC, Namespace wrote:
>> And why don't you call the function from your clone function?
>
> Because the body of the new function needs to see the 
> parameters as known at compile-time.
>
>> Maybe this could help you: http://dpaste.1azy.net/fork/597affd2
>> I used it to generate my own rvalue functions because of the 
>> lack of rvalue references.
>
> Thanks, that was helpful for a few hints, but it doesn't fix 
> this particular problem.

Oh, hey! I remember participating in writing some of that :)

Yeah, with traits, you can get enough info to extract all the 
information you want about the function to redeclare it any way 
you want.

The only thing you *can't* get is... the body!

If you have access to the source code of the function, you could 
redeclare it as a token string? eg:

before:

int foo(int a)
{
     return a+1;
}

after:
enum fooString =
q{
     return a+1;
};

int foo(int a){mixin(fooString);}
int foo()(int a){mixin(fooString);}

Yeah... not a great solution.

--------

The only way I could see it happen would be with a compile 
__trait ?

Does the compiler even have the info, or is it completely 
obliterated after the lex phase?

The compiler *should* have the info, since it can pinpoint the 
coordinates of compilation errors.

But I wouldn't know too much.


More information about the Digitalmars-d-learn mailing list