www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Getting all types defined in a module at compile time

reply Ben Jones <fake fake.fake> writes:
I'm trying to get a list of all the types defined in a module at 
compile time (enums, structs, classes).  I attempted to use the 
isAggregateType trait, but it choked when it was passed modules 
that had been imported (object, and the others explicitly 
imported).

My next attempt was to try to filter out the modules, then look 
at everything left over with things like isAggregateType, etc.  
However, I'm getting errors when trying to filter on isModule.

Am I missing out on a simpler/more direct approach?  Is there a 
workaround to this alias error?

```
module thismodule;

import std.meta;
import std.traits;

struct S{}
enum E {asdf};
class C{}

template symbols(alias mod){
	alias getSymbol(alias T) = __traits(getMember, mod, T);
     alias symbols = staticMap!(getSymbol, __traits(allMembers, 
mod));
}

template notmodule(alias T){
     alias notmodule = __traits(isModule, T);
}

void main(){
     static foreach(s; symbols!thismodule){
      	pragma(msg, fullyQualifiedName!s);
     }

     alias notmods = Filter!(templateNot!notmodule, 
symbols!thismodule);

}
```

I get errors:

```
(on the alias notmodule line) Error: trait isModule is either 
invalid or not supported in alias
```
Nov 18 2019
parent reply Paul Backus <snarwin gmail.com> writes:
On Monday, 18 November 2019 at 21:48:00 UTC, Ben Jones wrote:
 template notmodule(alias T){
     alias notmodule = __traits(isModule, T);
 }

 [...]

 I get errors:

 ```
 (on the alias notmodule line) Error: trait isModule is either 
 invalid or not supported in alias
 ```
The result of __traits(isModule, T) is a boolean value, so the line should be enum notmodule = __traits(isModule, T);
Nov 18 2019
parent Ben Jones <fake fake.fake> writes:
On Tuesday, 19 November 2019 at 01:55:17 UTC, Paul Backus wrote:
 On Monday, 18 November 2019 at 21:48:00 UTC, Ben Jones wrote:
 template notmodule(alias T){
     alias notmodule = __traits(isModule, T);
 }

 [...]

 I get errors:

 ```
 (on the alias notmodule line) Error: trait isModule is either 
 invalid or not supported in alias
 ```
The result of __traits(isModule, T) is a boolean value, so the line should be enum notmodule = __traits(isModule, T);
Thanks! Is there a good resource on D metaprogramming somewhere? I'm kind of stumbling my way through things and would love to see some best practices laid out somewhere.
Nov 18 2019