trait to get the body code of a function?

Mr.Bingo Bingo at Namo.com
Thu Jul 26 07:32:19 UTC 2018


On Tuesday, 24 July 2018 at 04:43:33 UTC, Guillaume Lathoud wrote:
> Hello,
>
> __traits and std.traits already offer access to function 
> information like input parameters, e.g. in std.traits: 
> ParameterIdentifierTuple ParameterStorageClassTuple
>
> Even if that might sound strange, is there a compile time 
> access to the body of the function, e.g. as a code string, or 
> at least to the filename where it was declared?
>
> I have not found a direct "string" access, but I found these:
>
> https://dlang.org/phobos/std_traits.html#moduleName
>
> https://dlang.org/phobos/std_traits.html#packageName
>
> ...and I am not sure how to proceed from here to get the code 
> as a string that declared the function. Context: I have some 
> compile-time ideas in mind (code transformation).
>
> Best regards,
> Guillaume Lathoud

If all you need is the string you can write a template function 
that imports the file and searches for the function and returns 
it's body.

It's not very robust but it can work for some cases. D really 
should allow one to get the function body in D, possibly in a 
type safe way(such as each line is parsed properly and return 
type info about what the line contains.


More information about the Digitalmars-d-learn mailing list