digitalmars.D.learn - Get all functions
- Andrea Fontana (6/6) Oct 08 2013 I have a number of functions like:
- Dicebot (2/8) Oct 08 2013 In specific module or in whole program?
- Dicebot (27/40) Oct 08 2013 Anyway, full answer:
- Andrea Fontana (2/43) Oct 08 2013
- Dicebot (5/6) Oct 08 2013 Yeah it is often overlooked but you can use module symbol in most
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
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
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: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.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
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: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.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
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