Using __traits to find functions in sub-modules

John Colvin via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Oct 16 12:19:19 PDT 2014


On Thursday, 16 October 2014 at 18:39:50 UTC, nrgyzer wrote:
> Hi,
> I'm using structs to describe my functions:
>
> struct example
> {
>    string name;
>    uint someValue;
> }
>
> module mod.example1;
>
> @example("example1", 1)
> void myFunction()
> {
> // do something
> }
>
> module mod.example2;
>
> @example("example2", 2)
> void myFunction()
> {
> // do something
> }
>
> I'm using the struct to describe functions in different 
> modules. Now, I want add all functions which are described 
> using the example-struct to an array during compile time. But 
> how can I do this? I know, I can use __trait(allMembers, 
> mod.example1) and __trait(allMembers, mod.example2), but I only 
> want specify the parent module (mod), for instance:
>
> void main()
> {
>    foreach (member, __traits(allMembers, mod))
>    {
>       writeln(member);
>    }
> }
>
> But this only shows "object" - nothing else. No sub-modules 
> like mod.example1 or mod.example2. So, how can I find all 
> functions that are described using my structure during compile 
> time and add them to an array?
>
> I already tried this:
>
> void main()
> {
>    foreach (cmodule; ModuleInfo)
>    {
>       foreach (submodule; __traits(allMembers, cmodule))
>       {
>       // ... also tried: foreach (submodule; 
> __traits(allMembers, mixin(cmodule.name))), cmodule.name is not 
> available during compile time...
>       }
>    }
> }
>
> But it always stats that 'cmodule' has no members. Does anyone 
> know how to solve the problem?

perhaps you could get somewhere by using a package.d in every 
package?

If it needs to work on packages you don't control then I don't 
really know :/


More information about the Digitalmars-d-learn mailing list