www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Listing all functions in a module

reply "Phil" <theotherphil hotmail.com> writes:
Is it possible to enumerate all functions in a module? I'd like 
my test program to find the functions in a fibrary with a 
particular attribute defined, but I can't find any way to do 
this. I've looked in std.traits; maybe I'm missing something.
Mar 23 2014
parent reply "Phil" <theotherphil hotmail.com> writes:
On Sunday, 23 March 2014 at 12:20:51 UTC, Phil wrote:
 Is it possible to enumerate all functions in a module? I'd like 
 my test program to find the functions in a fibrary with a 
 particular attribute defined, but I can't find any way to do 
 this. I've looked in std.traits; maybe I'm missing something.
Ah, allMembers does the trick. Probably could have spent a bit longer googling that one before posting!
Mar 23 2014
parent reply "Rikki Cattermole" <alphaglosined gmail.com> writes:
On Sunday, 23 March 2014 at 12:28:06 UTC, Phil wrote:
 On Sunday, 23 March 2014 at 12:20:51 UTC, Phil wrote:
 Is it possible to enumerate all functions in a module? I'd 
 like my test program to find the functions in a fibrary with a 
 particular attribute defined, but I can't find any way to do 
 this. I've looked in std.traits; maybe I'm missing something.
Ah, allMembers does the trick. Probably could have spent a bit longer googling that one before posting!
Well I just made this for you, so here it is anyway: module mymodule; import std.stdio : writeln; void main() { foreach(m; __traits(allMembers, mymodule)) { static if (__traits(isStaticFunction, __traits(getMember, mymodule, m))) { writeln(m); } } } void myfunc() { } Output: main myfunc
Mar 23 2014
next sibling parent reply Philippe Sigaud <philippe.sigaud gmail.com> writes:
On Sun, Mar 23, 2014 at 1:31 PM, Rikki Cattermole
<alphaglosined gmail.com> wrote:

 Well I just made this for you, so here it is anyway:
I wonder whether we could have a 'D recipes' page on the wiki, since some question appear regularly. Does one already exist?
Mar 23 2014
next sibling parent reply "Rikki Cattermole" <alphaglosined gmail.com> writes:
On Sunday, 23 March 2014 at 12:45:13 UTC, Philippe Sigaud wrote:
 On Sun, Mar 23, 2014 at 1:31 PM, Rikki Cattermole
 <alphaglosined gmail.com> wrote:

 Well I just made this for you, so here it is anyway:
I wonder whether we could have a 'D recipes' page on the wiki, since some question appear regularly. Does one already exist?
I don't know. Other than the Rosetta code stuff, I believe there is nothing [0]. However we need to work on D idioms and patterns as well as examples of code. If your wanting to lead please do :) [0] http://rosettacode.org/wiki/Category:D
Mar 23 2014
next sibling parent Philippe Sigaud <philippe.sigaud gmail.com> writes:
On Sun, Mar 23, 2014 at 1:52 PM, Rikki Cattermole
<alphaglosined gmail.com> wrote:


 I wonder whether we could have a 'D recipes' page on the wiki, since
 some question appear regularly. Does one already exist?
I don't know. Other than the Rosetta code stuff, I believe there is nothing [0]. However we need to work on D idioms and patterns as well as examples of code.
Ah, I found one: http://wiki.dlang.org/Cookbook Obviously, I didn't know this page existed.
 If your wanting to lead please do :)
Well, I'm trying to understand how to add a new recipe, with the correct tag.
Mar 23 2014
prev sibling parent reply Philippe Sigaud <philippe.sigaud gmail.com> writes:
 If your wanting to lead please do :)
Well, I'm trying to understand how to add a new recipe, with the correct tag.
There, here it is: http://wiki.dlang.org/Finding_all_Functions_in_a_Module Feel free to edit it!
Mar 23 2014
parent reply "Phil" <theotherphil hotmail.com> writes:
I'm a bit confused about how this example works. I thought 
foreach was a runtime loop, so how does nesting static ifs inside 
it work?
Mar 23 2014
parent "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
On Sunday, 23 March 2014 at 14:22:07 UTC, Phil wrote:
 I'm a bit confused about how this example works. I thought 
 foreach was a runtime loop, so how does nesting static ifs 
 inside it work?
foreach() over a tuple is unrolled at compile time.
Mar 23 2014
prev sibling parent "Adam D. Ruppe" <destructionator gmail.com> writes:
On Sunday, 23 March 2014 at 12:45:13 UTC, Philippe Sigaud wrote:
 I wonder whether we could have a 'D recipes' page on the wiki,
http://wiki.dlang.org/Cookbook Doesn't have much, but there's a start there.
Mar 23 2014
prev sibling parent "Phil" <theotherphil hotmail.com> writes:
On Sunday, 23 March 2014 at 12:31:47 UTC, Rikki Cattermole wrote:
 Well I just made this for you, so here it is anyway:
 ...
 	static if (__traits(isStaticFunction, __traits(getMember, 
 mymodule, m))) ...
Thanks, that's very helpful; it answered my next question which was going to be why __traits(someTrait, nameOfSymbolInAnotherModule) didn't work.
Mar 23 2014