www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Compile-Time module info

reply "Chris NS" <ibisbasenji gmail.com> writes:
Is there any means to get meaningful moduleinfo at compile time, 
specifically a list of the local classes of a given module?  
__traits(allMembers, mod) doesn't work: "inverse.d(109): Error: 
import data has no members".

I was hoping that something along these lines would be possible 
in CTFE:

foreach ( decl ; __traits( allMembers, data ) ) {
     if ( is( typeof( mixin( decl ) ) == class )
     && !__traits( isAbstractclass, mixin( decl ) ) ) {
         // ...some code in here...
     }
}


-- Chris NS
Jul 22 2012
parent Philippe Sigaud <philippe.sigaud gmail.com> writes:
On Sun, Jul 22, 2012 at 9:31 AM, Chris NS <ibisbasenji gmail.com> wrote:
 Is there any means to get meaningful moduleinfo at compile time,
 specifically a list of the local classes of a given module?
 __traits(allMembers, mod) doesn't work: "inverse.d(109): Error: import data
 has no members".

 I was hoping that something along these lines would be possible in CTFE:

 foreach ( decl ; __traits( allMembers, data ) ) {
     if ( is( typeof( mixin( decl ) ) == class )
     && !__traits( isAbstractclass, mixin( decl ) ) ) {
         // ...some code in here...
     }
 }
The catch is that __traits(allMembers, symbol) does not work on a simple module name (as in "module foo;"). It does work on composite module names (as in "module std.algorithm;" or "module mypackage.mymodule;"). I don't know if that's a bug or if that fact that __traits(allMembers, ...) works on a module is an unexpected loophole. Anyway, IIRC, I give code to do that in an online tutorial on templates, here: https://github.com/PhilippeSigaud/D-templates-tutorial/blob/master/dtemplates.pdf?raw=true The code predates a few recent addition to std.traits, so it could possibly made shorter now, I don't know. Read from p.114 (20.3) and following. I guess what you need is section 20.7, p. 118+. The code from all examples should be in a directory on Github, also. Philippe
Jul 22 2012