Thread overview | |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
March 23, 2014 Listing all functions in a module | ||||
---|---|---|---|---|
| ||||
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. |
March 23, 2014 Re: Listing all functions in a module | ||||
---|---|---|---|---|
| ||||
Posted in reply to Phil | 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!
|
March 23, 2014 Re: Listing all functions in a module | ||||
---|---|---|---|---|
| ||||
Posted in reply to Phil | 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
|
March 23, 2014 Re: Listing all functions in a module | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rikki Cattermole | 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?
|
March 23, 2014 Re: Listing all functions in a module | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rikki Cattermole | 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.
|
March 23, 2014 Re: Listing all functions in a module | ||||
---|---|---|---|---|
| ||||
Posted in reply to Philippe Sigaud | 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 |
March 23, 2014 Re: Listing all functions in a module | ||||
---|---|---|---|---|
| ||||
Posted in reply to Philippe Sigaud | 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. |
March 23, 2014 Re: Listing all functions in a module | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rikki Cattermole | 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. |
March 23, 2014 Re: Listing all functions in a module | ||||
---|---|---|---|---|
| ||||
>> 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! |
March 23, 2014 Re: Listing all functions in a module | ||||
---|---|---|---|---|
| ||||
Posted in reply to Philippe Sigaud | 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? |
Copyright © 1999-2021 by the D Language Foundation