www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - allMembers

reply Manu <turkeyman gmail.com> writes:
So I do a lot of module scanning via allMembers, and I'm consistently
running into an awkward problem:

module test.blah;

static this()
{
  foreach(m; __traits(allMembers, test.module))
  {
    // m is a string, the only way I know to address the thing m references
is: mixin(m), and this can't be involved in virtually any complex
expressions...

    // I need to know if m is an interface, class or struct
    static if( is( mixin(m) == [interface/class/struct] ) )   // how can I
do this? I can't use mixin() here.


    // I have a scenario where I need to know if m is a function, and I
made it work using an ugly trick like this:
    static if( is( typeof( mixin( m ) ) ) ) // this works!
    {
      // here, I know that m is an instance of something, which means I can
alias it's typeof()
      alias typeof( mixin( m ) ) Type;

      // ... and life is good
      static if( is( Type == function ) )
      {
        ...
      }
    }

    // that construct doesn't work for definitions, like
interface/class/struct

  }
}

What can I do?
May 04 2012
parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
__traits(getMember, test.module, m);

should work.
May 04 2012
next sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Fri, May 04, 2012 at 03:34:00PM +0200, Adam D. Ruppe wrote:
 __traits(getMember, test.module, m);
 
 should work.
Though you should probably test for non-data members before attempting to use getMember (try __traits(compiles, __traits(getMember, ...))). T -- Democracy: The triumph of popularity over principle. -- C.Bond
May 04 2012
prev sibling next sibling parent Manu <turkeyman gmail.com> writes:
On 4 May 2012 16:34, Adam D. Ruppe <destructionator gmail.com> wrote:

 __traits(getMember, test.module, m);

 should work.
Tried that: static if( is( __traits( getMember, mixin( moduleName ), m ) == interface ) ) { pragma( msg, "Is an interface: " ~ m ); }
May 04 2012
prev sibling next sibling parent Manu <turkeyman gmail.com> writes:
On 4 May 2012 17:01, Manu <turkeyman gmail.com> wrote:

 On 4 May 2012 16:34, Adam D. Ruppe <destructionator gmail.com> wrote:

 __traits(getMember, test.module, m);

 should work.
Tried that: static if( is( __traits( getMember, mixin( moduleName ), m ) == interface ) ) { pragma( msg, "Is an interface: " ~ m ); }
** accidental 'send' ** >_< Fails: engine\bindings.d(399):basic type expected, not __traits engine\bindings.d(399):found '__traits' when expecting ')' engine\bindings.d(399):expression expected, not 'interface' engine\bindings.d(399):found ')' instead of statement
May 04 2012
prev sibling next sibling parent Manu <turkeyman gmail.com> writes:
On 4 May 2012 17:01, H. S. Teoh <hsteoh quickfur.ath.cx> wrote:

 On Fri, May 04, 2012 at 03:34:00PM +0200, Adam D. Ruppe wrote:
 __traits(getMember, test.module, m);

 should work.
Though you should probably test for non-data members before attempting to use getMember (try __traits(compiles, __traits(getMember, ...))).
But... I'm trying to get non-data members. I need to match declarations...
May 04 2012
prev sibling next sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Fri, May 04, 2012 at 05:03:36PM +0300, Manu wrote:
 On 4 May 2012 17:01, Manu <turkeyman gmail.com> wrote:
 
 On 4 May 2012 16:34, Adam D. Ruppe <destructionator gmail.com> wrote:

 __traits(getMember, test.module, m);

 should work.
Tried that: static if( is( __traits( getMember, mixin( moduleName ), m ) == interface ) ) { pragma( msg, "Is an interface: " ~ m ); }
** accidental 'send' ** >_< Fails: engine\bindings.d(399):basic type expected, not __traits engine\bindings.d(399):found '__traits' when expecting ')' engine\bindings.d(399):expression expected, not 'interface' engine\bindings.d(399):found ')' instead of statement
You're missing a typeof there. __traits(getMember,...) returns the actual value of the member, not just its type. T -- Государство делает вид, что платит нам зарплату, а мы делаем вид, что работаем.
May 04 2012
prev sibling parent Manu <turkeyman gmail.com> writes:
On 4 May 2012 17:09, H. S. Teoh <hsteoh quickfur.ath.cx> wrote:

 On Fri, May 04, 2012 at 05:03:36PM +0300, Manu wrote:
 On 4 May 2012 17:01, Manu <turkeyman gmail.com> wrote:

 On 4 May 2012 16:34, Adam D. Ruppe <destructionator gmail.com> wrote:

 __traits(getMember, test.module, m);

 should work.
Tried that: static if( is( __traits( getMember, mixin( moduleName ), m ) ==
interface
 ) )
 {
   pragma( msg, "Is an interface: " ~ m );
 }
** accidental 'send' ** >_< Fails: engine\bindings.d(399):basic type expected, not __traits engine\bindings.d(399):found '__traits' when expecting ')' engine\bindings.d(399):expression expected, not 'interface' engine\bindings.d(399):found ')' instead of statement
You're missing a typeof there. __traits(getMember,...) returns the actual value of the member, not just its type.
Sorry, I meant to say above: I'm looking for _definitions_ (not declarations), and not variables.
May 04 2012