www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Get all functions

reply "Andrea Fontana" <nospam example.com> writes:
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?
Oct 08 2013
parent reply "Dicebot" <public dicebot.lv> writes:
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?
Oct 08 2013
parent reply "Dicebot" <public dicebot.lv> writes:
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.
Oct 08 2013
parent reply "Andrea Fontana" <nospam example.com> writes:
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.
Oct 08 2013
parent "Dicebot" <public dicebot.lv> writes:
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)
Oct 08 2013