Discussion:
Using __traits to find functions in sub-modules
nrgyzer via Digitalmars-d-learn
2014-10-16 18:39:48 UTC
Permalink
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?
ketmar via Digitalmars-d-learn
2014-10-16 19:00:52 UTC
Permalink
On Thu, 16 Oct 2014 18:39:48 +0000
Post by nrgyzer via Digitalmars-d-learn
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.
John Colvin via Digitalmars-d-learn
2014-10-16 19:19:19 UTC
Permalink
Post by nrgyzer via Digitalmars-d-learn
Hi,
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
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?
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 :/
nrgyzer via Digitalmars-d-learn
2014-10-17 17:34:36 UTC
Permalink
Post by John Colvin via Digitalmars-d-learn
Post by nrgyzer via Digitalmars-d-learn
Hi,
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
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?
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...

Loading...