Thread overview
Using __traits to find functions in sub-modules
Oct 16, 2014
nrgyzer
Oct 16, 2014
ketmar
Oct 16, 2014
John Colvin
Oct 17, 2014
nrgyzer
October 16, 2014
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?
October 16, 2014
On Thu, 16 Oct 2014 18:39:48 +0000
nrgyzer via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com>
wrote:

> But it always stats that 'cmodule' has no members. Does anyone know how to solve the problem?
there is no non-hackish solution, afaik. there is no even hackish, but reliable one.


October 16, 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 :/
October 17, 2014
On Thursday, 16 October 2014 at 19:19:21 UTC, John Colvin wrote:
> 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 :/

Hm, importing the package doesn't help :(
Okay, I see... I've to solve the problem using another idea...