Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
October 08, 2013 Get all functions | ||||
---|---|---|---|---|
| ||||
I have a number of functions like: @MyUda("...") void test(); And I want to check them (at compile time, of course) to generate the right calls. Is there a way to get a list of them? Using traits/reflection? |
October 08, 2013 Re: Get all functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrea Fontana | On Tuesday, 8 October 2013 at 12:45:48 UTC, Andrea Fontana wrote:
> I have a number of functions like:
>
> @MyUda("...")
> void test();
>
> And I want to check them (at compile time, of course) to generate the right calls.
>
>
> Is there a way to get a list of them? Using traits/reflection?
In specific module or in whole program?
|
October 08, 2013 Re: Get all functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dicebot | On Tuesday, 8 October 2013 at 13:45:42 UTC, Dicebot wrote:
> On Tuesday, 8 October 2013 at 12:45:48 UTC, Andrea Fontana wrote:
>> I have a number of functions like:
>>
>> @MyUda("...")
>> void test();
>>
>> And I want to check them (at compile time, of course) to generate the right calls.
>>
>>
>> Is there a way to get a list of them? Using traits/reflection?
>
> In specific module or in whole program?
Anyway, full answer:
1) For a given module it is very easy
```
foreach(name; __traits(allMembers, moduleName))
{
mixin ("alias symbol = " ~ name ~ ";");
static if (is(symbol == function))
{
alias UDAs = __traits(getAttributes, symbol);
foreach (UDA; UDAs)
{
static if (is(typeof(UDA) == MyUda))
// proceed as you wish
}
}
}
```
2)
To get all transitively accessible modules you need to use some `.stringof` magic. Imported modules / packages are also listed in `allMembers` tuple but only way to find those is to check if `symbol.stringof` starts with "package " or "module " and then call `__traits(allMembers, symbol) recursively.
Some of Phobos functions already do this and this should have probably been generalized but not progress there so far, at least one I am aware of.
|
October 08, 2013 Re: Get all functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dicebot | Oh, I didn't realize that allMembers works for modules too :)
On Tuesday, 8 October 2013 at 14:00:51 UTC, Dicebot wrote:
> On Tuesday, 8 October 2013 at 13:45:42 UTC, Dicebot wrote:
>> On Tuesday, 8 October 2013 at 12:45:48 UTC, Andrea Fontana wrote:
>>> I have a number of functions like:
>>>
>>> @MyUda("...")
>>> void test();
>>>
>>> And I want to check them (at compile time, of course) to generate the right calls.
>>>
>>>
>>> Is there a way to get a list of them? Using traits/reflection?
>>
>> In specific module or in whole program?
>
> Anyway, full answer:
>
> 1) For a given module it is very easy
> ```
> foreach(name; __traits(allMembers, moduleName))
> {
> mixin ("alias symbol = " ~ name ~ ";");
> static if (is(symbol == function))
> {
> alias UDAs = __traits(getAttributes, symbol);
> foreach (UDA; UDAs)
> {
> static if (is(typeof(UDA) == MyUda))
> // proceed as you wish
> }
> }
> }
> ```
>
> 2)
> To get all transitively accessible modules you need to use some `.stringof` magic. Imported modules / packages are also listed in `allMembers` tuple but only way to find those is to check if `symbol.stringof` starts with "package " or "module " and then call `__traits(allMembers, symbol) recursively.
>
> Some of Phobos functions already do this and this should have probably been generalized but not progress there so far, at least one I am aware of.
|
October 08, 2013 Re: Get all functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrea Fontana | On Tuesday, 8 October 2013 at 14:09:49 UTC, Andrea Fontana wrote:
> Oh, I didn't realize that allMembers works for modules too :)
>
Yeah it is often overlooked but you can use module symbol in most contexts you would use any other normal symbol. It is only special in a sense that it lacks type you can check with `is` statement (thus .stringof hack)
|
Copyright © 1999-2021 by the D Language Foundation