Get all functions

Dicebot public at dicebot.lv
Tue Oct 8 07:00:50 PDT 2013


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.


More information about the Digitalmars-d-learn mailing list