Thread overview | |||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
July 24, 2018 trait to get the body code of a function? | ||||
---|---|---|---|---|
| ||||
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 |
July 24, 2018 Re: trait to get the body code of a function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Guillaume Lathoud | On 24/07/2018 4:43 PM, 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
Doesn't exist.
|
July 24, 2018 Re: trait to get the body code of a function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Guillaume Lathoud | 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 Not sure if this could be of interest: https://forum.dlang.org/thread/ifllo2$a45$1@digitalmars.com |
July 24, 2018 Re: trait to get the body code of a function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to rikki cattermole | On Tuesday, 24 July 2018 at 05:27:36 UTC, rikki cattermole wrote:
> On 24/07/2018 4:43 PM, Guillaume Lathoud wrote:
>> [...]
>
> Doesn't exist.
Well, IMO it should exist as it can be quite useful for generating metainfo.
|
July 24, 2018 Re: trait to get the body code of a function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to 12345swordy | On Tuesday, 24 July 2018 at 14:26:45 UTC, 12345swordy wrote:
> On Tuesday, 24 July 2018 at 05:27:36 UTC, rikki cattermole wrote:
>> On 24/07/2018 4:43 PM, Guillaume Lathoud wrote:
>>> [...]
>>
>> Doesn't exist.
>
> Well, IMO it should exist as it can be quite useful for generating metainfo.
Thanks for all the answers. I've just had a look at an alternative: using dmd as a package. However that's a lot of doc to browse... Maybe someone has experience with dmd as a package?
Otherwise I'll just stick to strings, but it'd be nice to have.
Best regards,
Guillaume
|
July 25, 2018 Re: trait to get the body code of a function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Guillaume Lathoud | On 25/07/2018 5:32 AM, Guillaume Lathoud wrote:
> Thanks for all the answers. I've just had a look at an alternative: using dmd as a package. However that's a lot of doc to browse... Maybe someone has experience with dmd as a package?
Not a solution. Leaks memory, not reusable and in general not very friendly to work with.
|
July 25, 2018 Re: trait to get the body code of a function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to rikki cattermole | On Wednesday, 25 July 2018 at 04:46:20 UTC, rikki cattermole wrote:
> On 25/07/2018 5:32 AM, Guillaume Lathoud wrote:
>> Thanks for all the answers. I've just had a look at an alternative: using dmd as a package. However that's a lot of doc to browse... Maybe someone has experience with dmd as a package?
>
> Not a solution. Leaks memory, not reusable and in general not very friendly to work with.
Thanks.
|
July 25, 2018 Re: trait to get the body code of a function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Guillaume Lathoud | On Wednesday, 25 July 2018 at 19:05:08 UTC, Guillaume Lathoud wrote:
> On Wednesday, 25 July 2018 at 04:46:20 UTC, rikki cattermole wrote:
>> On 25/07/2018 5:32 AM, Guillaume Lathoud wrote:
>>> Thanks for all the answers. I've just had a look at an alternative: using dmd as a package. However that's a lot of doc to browse... Maybe someone has experience with dmd as a package?
>>
>> Not a solution. Leaks memory, not reusable and in general not very friendly to work with.
>
> Thanks.
Please submit a bug report to Bugzilla on this, as I am also interest in this.
Alexander
|
July 26, 2018 Re: trait to get the body code of a function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Guillaume Lathoud | 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.
|
July 26, 2018 Re: trait to get the body code of a function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mr.Bingo | On Thursday, 26 July 2018 at 07:32:19 UTC, Mr.Bingo wrote: > 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. I would argue, just because of "type safety" you mentioned, D should not allow one to get the function body. The type safety is not achievable because of https://en.wikipedia.org/wiki/Lambda_calculus#Undecidability_of_equivalence The string itself could be useful however... Whatever OP has in mind with this string... |
Copyright © 1999-2021 by the D Language Foundation